TreeView and binding my class

2 posts, 0 answers
  1. sharp
    sharp avatar
    27 posts
    Member since:
    Feb 2011

    Posted 19 Apr 2011 Link to this post

    Hi.
    I have class
    public class User
    {
    public int Id {get; set;}
    public string Name{get;set;}
    public string phone{get;set;}
    public List<User> userList {get; set;}
    }
    How can I bind list with this type data to treeView.
    I've try  this:
    <telerik:RadTreeView x:Name="userTree" >
                        <telerik:RadTreeView.ItemTemplate>
                            <telerik:HierarchicalDataTemplate>
                                <TextBlock Text="{Binding Name}"/>       
                            </telerik:HierarchicalDataTemplate>
                        </telerik:RadTreeView.ItemTemplate>
     
                    </telerik:RadTreeView>
     But I see only root element.
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 20 Apr 2011 Link to this post

    Hi sharp,

    You have to set the ItemsSource of the HierarchicalDataTemplate like so:
    <telerik:RadTreeView.ItemTemplate>
                            <telerik:HierarchicalDataTemplate ItemsSource={Binding userList}>
                                <TextBlock Text="{Binding Name}"/>        
                            </telerik:HierarchicalDataTemplate>
                        </telerik:RadTreeView.ItemTemplate>
    You can also check out the attached project and this help article if you need more info. Let us know if this helped you. Additionally, it is better practice to use ObservableCollection instead of List since it inherits from INotifyCollectionChanged and INotifyPropertyChanged and this way every change in the ViewModel will be automatically reflected in the UI.

    Kind regards,
    Petar Mladenov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
Back to Top