Need example of how to expand all children of a given node using load on demand

2 posts, 0 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    148 posts
    Member since:
    Nov 2008

    Posted 12 Jun 2017 Link to this post


    Given a RadTreeViewItem, I want to expand all if it's descendants. I receive the first RadTreeViewItem via an attached behavior:


    private void OnLoaded(object sender, RoutedEventArgs e)
              RadTreeViewItem tvi = AssociatedObject;


    after doing some checks I call tvi.IsExpanded = True and tvi.IsSelected = True;

    This gets my tree opened up to the desired node. Now I want to expand all of it's children so the user doesn't have to open up each branch by hand.


    01.private void ExpandChildren(RadTreeViewItem tvItem)
    04.    //var container = tvItem.ItemContainerGenerator.IndexFromContainer();
    05.    var tvi = tvItem;
    06.    foreach (RadTreeViewItem item in tvi.Items)
    07.    {
    08.        var kid = tvi.ParentTreeView.ContainerFromItemRecursive(item);
    09.        kid.IsExpanded = true;
    10.        if (kid.HasItems)
    11.            ExpandChildren(kid);
    12.    }


    However, this function blows up on line 6 complaining that the item cannot be converted from the value (in my case an app specific class) type to a RadTreeViewItem. I tried using ContainerFromItemRecursive, but it always seems to return null. How to convert a tvi.Items[n] to a RadTreeViewItem that I can mark IsExpanded = true?

    This seems like this shouldn't be that hard.

    Can someone please point me in the right direction?

    Thanks ... Ed


  2. Martin Ivanov
    Martin Ivanov avatar
    2425 posts

    Posted 14 Jun 2017 Link to this post

    Hello Ed,

    The RadTreeView containers of some of your business object are not found probably because they are not yet generated when the ContainerFromItemRecursive() method is called. 

    To achieve your requirement you can define a boolean property holding the expand state of the item and bind it to the IsExpanded property of RadTreeViewItem. In data binding scenario you can do that via a Style. Check the Expanding and Collapsing Items help article. When you bind the property you can set it in on the model in your ExpandChildren() method.

    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.
Back to Top