This is a migrated thread and some comments may be shown as answers.

inherit RadTreeViewItem

1 Answer 82 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 23 Apr 2018, 06:05 AM

hello,

i am new to the telerik controls and i have a question to the radtreeview.

I have two normal classes and a hierarichal template. Everything works fine and the data is shown in tree.

Instead of the normal class i want to use the radtreeviewitem. I try to inherit wich works for the nodes but

not for the childs. Can you please give me a advice how to do this. Here is my code and my problem is

the Children property in the class recursiveObject. Thank you and greetings ....

public static ObservableCollection<RecursiveObject> FillRecursive(List<FlatObject> flatObjects, int parentId)
{
    ObservableCollection<RecursiveObject> recursiveObjects = new ObservableCollection<RecursiveObject>();
    foreach (var item in flatObjects.Where(x => x.ParentId.Equals(parentId)))
    {
        recursiveObjects.Add(new RecursiveObject
        {
            Data = item.Data,
            NodeId = item.Id,
            Menge = item.Menge,
            Selected = false,
            ParentKey = item.ParentId,
            Children = FillRecursive(flatObjects, item.Id)
        });
    }
    return recursiveObjects;
}
 
public class FlatObject
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Data { get; set; }
    public int Menge { get; set; }
 
    public FlatObject(string name, int id, int parentId, int menge)
    {
        Data = name;
        Id = id;
        ParentId = parentId;
        Menge = menge;
    }
}
 
public class RecursiveObject : RadTreeViewItem
{
    public bool Selected { get; set; }
    public int NodeId { get; set; }
    public int ParentKey { get; set; }
    public string Data { get; set; }
    public int Menge { get; set; }
    public ObservableCollection<RecursiveObject> Children { get; set; }
}

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 25 Apr 2018, 12:48 PM
Greetings Richard,

Can you please tell me what exactly you mean by saying that it doesn't work for the children? Are they not visualized or there is some other issue? Can you tell me how you populate the treeview?

At first sight it looks like you are using the RadTreeView control in a data binding scenario (setting its ItemsSource). If that's the case please keep in mind that you will need to use the HierarchicalDataTemplate class to visualize the hierarchy properly. Also, you will need to set the ItemsSource property of the hierarchical template. You can read more about this in the Hierarchical Data Templates article.

Also, if you are data binding the treeview, I wouldn't recommend you to populate it with RadTreeViewItem elements. You can do this of course, but you don't need to. Firstly, RadTreeViewItem is an UIElement and its original idea is to present UI. Secondly, RadTreeViewItem has a bunch of properties and methods that would be unusable and will only occupy the memory. Instead, I would recommend you to use a custom business object where you can define whatever properties you like and then bind them to the RadTreeViewItem elements that will be automatically generate by the treeview when loaded.

Anyway, I would ask you also to tell me why you need to inherit the RadTreeViewItem control. This will give me better idea of what you are trying to achieve and I can think what to suggest.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
TreeView
Asked by
Richard
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or