Bill Henning

Does anyone happen to know how to retrieve the immediate next or previous item in a TreeView relative to the selected item Basically I want to find the siblings of the selected TreeViewItem.




Re: Windows Presentation Foundation (WPF) Getting the next sibling of the selected TreeViewItem

Dr. WPF

Do you truly want siblings What if the TVI has no siblings (i.e., if the selected TVI happens to be a leaf node) Then would you be looking for the parent and it's next sibling or would you want nothing at all

If you already have a reference to the selected TVI and you truly want siblings, then you should walk your visual ancestors to find your parent ItemsControl (which will either be another TreeViewItem or the actual TreeView, depending on your level in the tree). Then you can use your parent's ItemContainerGenerator.GetIndexFromContainer() to find the index of the selected TVI. Once you have the index, you can easily get the siblings using your parent's ItemContainerGenerator.GetContainerFromIndex() for n-1 and n+1.






Re: Windows Presentation Foundation (WPF) Getting the next sibling of the selected TreeViewItem

Bill Henning

I was hoping TVI had properties like WinForm's TreeNode did for NextNode, etc. Where NextNode would return null if there wasn't one. But I will try what you suggested. Thanks!






Re: Windows Presentation Foundation (WPF) Getting the next sibling of the selected TreeViewItem

Dr. WPF

Sounds like a good v2 request! It wouldn't be too difficult for Microsoft to implement a static TreeViewHelper class for dealing with these kinds of issues and it would save us all some grunt work.