Completed
Last Updated: 08 Apr 2016 11:27 by ADMIN
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 27 May 2015 10:26
Category: TreeView
Type: Bug Report
1
FIX. RadTreeView - ArgumentException when binding to Object-relational data and specifying the CheckedMember
Error: "Object of type 'Telerik.WinControls.Enumerations.ToggleState' cannot be converted to type 'System.Boolean'."

To reproduce:

public Form1()
{
    InitializeComponent();

    List<Parent> dataItems = new List<Parent>();
    Parent currentParent;
    Child currentChild;
    List<Child> children;
    string parentId = string.Empty;
    string childId = string.Empty;
    for (int i = 1; i <= 5; i++)
    {
        parentId = Guid.NewGuid().ToString();

        children = new List<Child>();
        for (int j = 1; j < 5; j++)
        {
            childId = Guid.NewGuid().ToString();
            currentChild = new Child(childId, parentId, "SubNode." + i + "." + j, j % 2 == 0);
            children.Add(currentChild);
        }
        currentParent = new Parent(parentId, "Node." + i, i % 2 == 0,children);
        dataItems.Add(currentParent);
    }

    radTreeView1.DataSource = dataItems;
    radTreeView1.DisplayMember = "Title\\Name";
    radTreeView1.ChildMember = "Parent\\Children";
    radTreeView1.CheckedMember = "IsActive\\Status";
    radTreeView1.CheckBoxes = true;
}

public class Parent 
{
    public string ParentId { get; set; }

    public string Title { get; set; }

    public bool IsActive { get; set; }

    public List<Child> Children { get; set; }

    public Parent(string parentId, string title, bool isActive, List<Child> children)
    {
        this.ParentId = parentId;
        this.Title = title;
        this.IsActive = isActive;
        this.Children = children;
    }
}

public class Child
{
    public string ChildId { get; set; }

    public string ParentId { get; set; }

    public string Name { get; set; }

    public bool Status { get; set; }

    public Child(string childId, string parentId, string name, bool status)
    {
        this.ChildId = childId;
        this.ParentId = parentId;
        this.Name = name;
        this.Status = status;
    }
}

Workaround: Use a custom TypeConverter for the boolean properties:

public class ToggleStateConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(ToggleState);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is bool)
        {
            switch ((bool)value)
            {
                case true:
                    return ToggleState.On;
                case false:
                    return ToggleState.Off;
                default :
                    return ToggleState.Indeterminate;
            }
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(ToggleState);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        ToggleState state = (ToggleState)value;

        switch (state)
        {
            case ToggleState.On:
                return true;
            case ToggleState.Off:
                return false;
            case ToggleState.Indeterminate:
                return false;
        }

        return base.ConvertFrom(context, culture, value);
    }
}
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments