Expand items in TreeView with Hierarchical Template

2 posts, 1 answers
  1. Miguel Rios
    Miguel Rios avatar
    21 posts
    Member since:
    Sep 2009

    Posted 12 Mar 2010 Link to this post

    Hi to all,

    I am actually working in a project that includes a RadTreeView and i have a question about it... I bind the ItemsSource property of the TreeView to a Hierarchical collection previously elaborated, the collection objects have the properties string Nombre, bool IsSelected, obscollection<myObject> Children among others... but those are importants. Now, i use the next Hierarchical Template:

            <telerik:HierarchicalDataTemplate  
                x:Key="HTemplate" 
                telerik:ContainerBinding.ContainerBindings="{StaticResource BindingsCollection}" 
                ItemsSource="{Binding Children}">             
                <StackPanel Orientation="Horizontal"
                    <CheckBox x:Name="checky" Tag="{Binding Codigo}" Content="{Binding Nombre}"  IsChecked="{Binding IsSelected, Mode=TwoWay}"/> 
                </StackPanel> 
            </telerik:HierarchicalDataTemplate> 

    and the next BindingCollection:

                <telerik:ContainerBindingCollection x:Name="BindingsCollection"
                    <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding IsSelected, Mode=TwoWay}" /> 
            </telerik:ContainerBindingCollection> 


    Now, i was wondering, how could i expand the path to certain object that have the property IsSelected = true at the time the treeview is loaded?

    Something like this....

    -Node1
    -Node4
    -Node6
    -Node7
    -Object.IsSelected = true;
    -Node5
    -Node2
    -Node3


    Also, can i select multiple objects at the same time? And how hard would it be to perform a Search in this hierarchical collection, highlighting and expanding the parent nodes the objects that corresponds to the search parameterS?


    I would really appreciate some help with this... hope you have some ideas...

    Greetins!
  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3299 posts

    Posted 18 Mar 2010 Link to this post

    Hi Miguel Rios,

    The easiest way to bind the RadTreeViewItem's IsExpanded property to a property of your custom business object is to use ContainerBindingCollection. So if you want to expand certain items of your RadTreeView depending on the value of a boolean property  of your business object (for example, named IsExpanded), you only need to add the following binding collection:
    <telerik:ContainerBinding PropertyName="IsExpanded"
                                                    Binding="{Binding IsExpanded, Mode=TwoWay}"/>
     

    If you want to change the selection mode of the RadTreeView to multiple, you need to set the SelectionMode property to Multiple. That will allow the user to select multiple items at one time by holding down the control key and clicking multiple items with the mouse or by using the keyboard. 

    As for searching through the items of the RadTreeView, you can create a method, that traverse all the items of the tree. When the searched item is found  you can set the IsExpanded property of the displayed business object to True. Then you can set the background of the item to highlight it.

    Please take a look at the example I attached and let me know if it helps or if you need more info.

    All the best,
    Tina Stancheva
    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.
Back to Top