Expand all parent of programmatically select RadTreeViewItem

1 Answer 86 Views
TreeListView TreeView
Genachats
Top achievements
Rank 1
Iron
Genachats asked on 03 Mar 2022, 02:33 PM

Hello,

 

I'm trying to expand all parent of my XmlElement in my RadTreeView. I can't add a specific property on my binded elemnt because it's an XmlNode. I am trying to use onSelected or onSelectionChanged events but can't have a concluent result because i can't find the whole path if parent is collapsed.

 

Thank you

Martin Ivanov
Telerik team
commented on 07 Mar 2022, 12:25 PM

You can access only the generated RadTreeView items. If the UI virtualization is enabled on your side, then those will be only the items that are currently visible into the viewport. If the UI virtualization is disabled, then all items will be generated except the children of the items that weren't expanded yet. Once expanded, the child items are generated and you can access them even if the parent is collapsed afterwards. 

If you send over some code showing your data structure and RadTreeView setup, I will take a look and see if I can suggest a proper solution.

Genachats
Top achievements
Rank 1
Iron
commented on 08 Mar 2022, 09:47 AM

Thank you for your answer. Here some of my code :

<TreeView Name="DomTreeview"
		  Background="AliceBlue"
		  ItemsSource="{Binding Dom}"
		  ItemTemplate="{StaticResource NodeTemplate}">
	<b:Interaction.Behaviors>
		<behavior:TreeViewBehavior/>
	</b:Interaction.Behaviors>
</TreeView>


public ObservableCollection<XmlNode> Dom { get; set; } = new();

Code i've tested but not concluent:


    private async void DomTreeview_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count == 0 || e.AddedItems[0] is not XmlElement element)
            return;
        var path = XmlPathFinder.FindXPath(element);
        if (string.IsNullOrEmpty(path))
            return;
        if (path.Split("/")
                .Where(x => !string.IsNullOrEmpty(x))
                .Select(x => x.Split("["))
                .Select(x => new Tuple<string, int>(x[0], int.Parse(x[1][..^1]))) is not { } parentIds)
            return;

        parentIds = parentIds.SkipLast(1);
        var id = 1;
        var innerId = 1;
        var item = ((RadTreeView)sender).ItemContainerGenerator.ContainerFromIndex(id - 1) as RadTreeViewItem;
        foreach (var parentId in parentIds)
        {
            while (true)
            {
                await Task.Run(() =>
                {
                    var watcher = Stopwatch.StartNew();
                    while (item == null && watcher.ElapsedMilliseconds < 2000)
                        item = ((RadTreeView)sender).ItemContainerGenerator.ContainerFromIndex(id - 1) as RadTreeViewItem;
                });
                if (item == null)
                    return;
                if(item.Item is not XmlElement node || node.Name != parentId.Item1 || innerId != parentId.Item2)
                {
                    innerId++;
                    continue;
                }
                break;
            }
            item.IsExpanded = true;
            item.IsSelected = true;
            id++;
        }
    }

Martin Ivanov
Telerik team
commented on 11 Mar 2022, 08:57 AM

Thank you for the code. Note that the ItemContainerGenerator of the RadTreeView control will return only containers from the first level in the hierarchy. To get containers for each next level you will need to  use the ItemContainerGenerator of the parent RadTreeViewItem control. You can find one way to implement this type of search in the Iterate Through TreeViewItems article.

Additionally, you can save some code related to the search using the ContainerFromItemRecursive() method of RadTreeView. The method internally implements the iteration of the containers and tries to find the corresponding container through all levels in the hierarchy. For example:

var container = this.treeView.ContainerFromItemRecursive(searchItem);  

Genachats
Top achievements
Rank 1
Iron
commented on 24 Mar 2022, 10:25 AM

Hello sorry for the delay !

First of all thank you for your answer. The problem is still here, if the treeview doesn't show the node "ContainerFromItemRecursive" returns null. So i can't open its parents.

Martin Ivanov
Telerik team
commented on 25 Mar 2022, 12:01 PM

If you send me a runnable sample project I will take a look. 

1 Answer, 1 is accepted

Sort by
0
Genachats
Top achievements
Rank 1
Iron
answered on 24 Mar 2022, 04:22 PM

I have gave up. I am using of attribute from XmlNode.Attributes for binding IsExpand property.

 

Thanks :)

Martin Ivanov
Telerik team
commented on 25 Mar 2022, 12:02 PM

Good to hear that you managed to achieve this. In my opinion, the binding to the IsEpxanded property is more reliable in a data binding scenario.
Tags
TreeListView TreeView
Asked by
Genachats
Top achievements
Rank 1
Iron
Answers by
Genachats
Top achievements
Rank 1
Iron
Share this question
or