Check a Child Node and Expand all Parents

3 posts, 0 answers
    WILLIAM avatar
    134 posts
    Member since:
    Jun 2012

    Posted 08 Aug 2012 Link to this post

    In my RadTree, there is a node that I need to be checked when the view initially shown.  I have been able to accomplish this part, but I have not been able to expand all of its parents.  For the parents, I have been able to set their checked state to intermediate and calling BringToView, but the parents are still not expanding.

    I'm subscribing to the ItemPrepared event to select the checked state of the child and parents.

    Wouldn't a method to expand the note be easier?  Item.Expand()

    Also, I've read in other posts that you have to wait until the Items have been loaded, that makes sense, but what event do you subscribe to that lets you know that the items have been loaded?  ItemsLoaded event? or Items.CollectionChanged?

    <telerik:ContainerBindingCollection x:Key="TreeViewItemContainerBinding">
        <telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding Path=IsActive, Mode=TwoWay}" />
        <telerik:ContainerBinding PropertyName="IsSelected" Binding="{Binding Path=IsActive}" />

    <telerik:HierarchicalDataTemplate x:Key="OrgUnitsItemTemplate" ItemsSource="{Binding OrgUnits}"
         telerik:ContainerBinding.ContainerBindings ="{StaticResource TreeViewItemContainerBinding}">
         <StackPanel Orientation="Horizontal">
             <telerik:Label Content="{Binding ParentOrgUnit.Description}" />

    void UxOrgUnitsItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
                var item = e.PreparedItem.Item as OrgUnitHierarchy;
                if (item == null || item.ParentOrgUnit.OrgUnitId != Dto.CurrentOrgUnitId) return;
                e.PreparedItem.CheckState = ToggleState.On;
                var parentItem = e.PreparedItem.ParentItem;
                while (parentItem != null)
                    parentItem.CheckState = ToggleState.Indeterminate;
                    parentItem.IsExpanded = true;
                    parentItem = parentItem.ParentItem;
    WILLIAM avatar
    134 posts
    Member since:
    Jun 2012

    Posted 12 Aug 2012 Link to this post

    Is this not possible?  I think this would be common functionality.  I understand where you say that the items have to be bound before working with them.  But there is no event let inform the developer that these properties have been set or changed.  So how do you know when this is happened?  I've subscribed to a lot of the events, but the ItemsSource is not set yet.  Even the Loaded event is fired before this is set.

  3. DevCraft R3 2016 release webinar banner
  4. Petar Mladenov
    Petar Mladenov avatar
    2882 posts

    Posted 13 Aug 2012 Link to this post

    Hi William,

     Initially the RadTreeView generates / prepares only its root level RadTreeViewItems. Later when you expand a node, the RadTreeView generates the child items of the expanded item too. Expanding a particular RadTreeView item initially could be performed in various ways:
    1) Using the ExpandAll() method - potentially very expensive method and should be used in very small trees.
    2) Using the ExpandItemByPath method. It works similar to the GetItemByPath method described here. You give the path to the item and the tree will Generate the containers (RadtreeViewItems) on its way to the desired item. It might be expensive too because items you won't need to be expanded are being expanded too.
    3) Binding the IsExpanded property of the RadTreeViewItem. This the most "MVVM"-like and the cheapest solution.
    If you expose boolean property in your ViewModels you can bind it to the IsExpanded property  
    of the RadTreeViewItems with ContainerBingins (Silverlight4 ) or StyleBindings (Silverlight 5). Suchapproach is demonstrated in this help article.

    All the best,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top