Completed
Last Updated: 05 Nov 2015 12:22 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 28 Oct 2015 10:50
Category: TreeView
Type: Bug Report
0
FIX. RadTreeView - blank nodes after adding a new record to the DataSource
To reproduce:

public BlankTreeView()
{
    InitializeComponent();
    RadContextMenu menu = new RadContextMenu();
    RadMenuItem addItem = new RadMenuItem("Add");
    addItem.Click += addItem_Click;
    menu.Items.Add(addItem);
    this.radTreeView1.RadContextMenu = menu;
    
    BindingList<ParentObject> items = new BindingList<ParentObject>();
    for (int i = 0; i < 3; i++)
    {
        BindingList<ChildObject> subItems = new BindingList<ChildObject>();
        for (int j = 0; j < 10; j++)
        {
            subItems.Add(new ChildObject(j,"SubNode" + i + "." + j));
        }

        items.Add(new ParentObject(i ,"Node" + i,subItems));
    }

    this.radTreeView1.DataSource = items;
    this.radTreeView1.DisplayMember = "Title\\Description";
    this.radTreeView1.ChildMember = "ParentObject\\Children";
}

private void addItem_Click(object sender, EventArgs e)
{
    if (this.radTreeView1.SelectedNode != null)
    {
        if (this.radTreeView1.SelectedNode.Level == 0)
        {
            Random rand = new Random(); 
            ParentObject dataItem = this.radTreeView1.SelectedNode.DataBoundItem as ParentObject;
            if (dataItem != null)
            {
                dataItem.Children.Add(new ChildObject(rand.Next(0, 100),"New Node"));
            } 
        }
    }
}

public class ParentObject: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    private int id;
    private string title;
    private BindingList<ChildObject> children;

    public int ID
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
            OnPropertyChanged("ID");
        }
    }

    public string Title
    {
        get
        {
            return this.title;
        }
        set
        {
            this.title = value;
            OnPropertyChanged("Title");
        }
    }

    public BindingList<ChildObject> Children
    {
        get
        {
            return this.children;
        }
        set
        {
            this.children = value;
            OnPropertyChanged("Children");
        }
    }

    public ParentObject(int iD, string title, BindingList<ChildObject> children)
    {
        this.ID = iD;
        this.Title = title;
        this.Children = children;
    }
}

public class ChildObject: INotifyPropertyChanged
{ 
    public event PropertyChangedEventHandler PropertyChanged;

    private int id;
    private string description;

    public int ID
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
            OnPropertyChanged("ID");
        }
    }

    public string Description
    {
        get
        {
            return this.description;
        }
        set
        {
            this.description = value;
            OnPropertyChanged("Description");
        }
    }

    public ChildObject(int iD, string description)
    {
        this.id = iD;
        this.description = description;
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Workaround: reset the TreeViewElement after adding the record:

radTreeView1.TreeViewElement.Update(RadTreeViewElement.UpdateActions.Reset);
Attached Files:
0 comments