Getting the next sibling of the selected TreeViewItem
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.
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.
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.
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!