This is a migrated thread and some comments may be shown as answers.

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

1 Answer 188 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 12 Jun 2017, 06:44 AM

Hi,

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)
02.{
03.     
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.    }
13.}

 

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

 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 14 Jun 2017, 10:06 AM
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.

Regards,
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.
Tags
TreeView
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or