RadTree adding root node manually

2 posts, 0 answers
  1. Prakash
    Prakash avatar
    19 posts
    Member since:
    Oct 2010

    Posted 17 Oct 2011 Link to this post

    Hi Everyone,

    Following is my requirement,

    I'll get a list of names from the service as a collection and need to bind this collection to a RadTreeView and I need to add a root node named "List of Names". Plz help me in doing this and it's very urgent. 

     My Code:

    public

     

     

    class Employees

     

    {

     

     

        public string Name { get; set; }

     

    }



    List

     

     

    <Employees> items = new List<Employees>();

     

    items.Add(

     

    new Employees { Name = "name1" });

     

    items.Add(

     

    new Employees { Name = "name2" });

     

     

     

    Employees item1 = new Employees { Name = "name3" };

     

    items.Add(item1);

    NameTree.ItemsSource = items;


    <

     

     

    telerik:RadTreeView x:Name="NameTree" Margin="0" Grid.Row="0" ItemsSource="{Binding }" BorderThickness="0">

     

     

     

     

    <telerik:RadTreeView.ItemTemplate>

     

     

     

     

    <telerik:HierarchicalDataTemplate >

     

     

     

     

    <TextBlock Text="{Binding Name}"/>

     

     

     

     

    </telerik:HierarchicalDataTemplate>

     

     

     

     

    </telerik:RadTreeView.ItemTemplate>

     

     

     

     

    </telerik:RadTreeView>

     


    Thanks,
    Prakash.

  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    2943 posts
    Member since:
    Apr 2014

    Posted 20 Oct 2011 Link to this post

    Hi Prakash,

    Instead of populating the RadTreeView ItemsSource collection with the list of Employee names, you can create a RadTreeViewItem with a Header - List of names and populate its ItemsSource collection:

    <telerik:RadTreeView x:Name="NameTree" Margin="0" BorderThickness="0">
        <telerik:RadTreeViewItem Header="List of Names" ItemsSource="{Binding Employees}">
            <telerik:RadTreeViewItem.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
            </telerik:RadTreeViewItem.ItemTemplate>
        </telerik:RadTreeViewItem>
    </telerik:RadTreeView>

    Also when you use the RadTreeView control in a databinding scenario, please keep in mind the following notes:
    • If you want to be able to take advantage of  features like the drag/drop feature of the control, you need to bind its ItemsSource to an items collection that implements the INotifyCollectionChanged. Such a collection is the ObservableCollection. The List collection doesn't implement the INotifyCollectionChanged interface so when you populate the RadTreeView ItemsSource collection to a List collection, some of the RadTreeView features may not work properly.
    • You can display both a flat and a hierarchical collection in the RadTreeView control. In case you need to display a flat collection of items (with one level of items) then you can define a DataTemplate as the RadTreeView.ItemTemplate property. However, if you want to display a hierarchical collection you'll need to use a HierarchicalDataTemplate as the RadTreeView.ItemTemplate. The HierarchicalDataTemplate exposes properties as ItemsSource - the children collection that will be displayed and ItemTemplate - the template that should be applied to the children collection of items.
    • You can set the ItemsSource either in code-behind: radTreeView.ItemsSource = myCollection; or you can bind it to a collection that is defined in the DataContext of the RadTreeView:
    <UserControl>
     <UserControl.DataContext>
       <local:MainViewModel/>
     </UserControl.DataContext>
     <telerik:RadTreeView ItemsSource={Binding myCollection} .../>
     ...
    </UserControl>

    I attached a sample demonstrating how to implement your scenario as well as illustrating the notes above. I hope it helps. Please let us know if we can further assist you.

    Kind regards,
    Tina Stancheva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Download a free trial of UI for WPF
Back to Top