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
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.
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++; } }
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);
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.