Given a random TreeViewItem from a tree, how do I retrieve the granddaddy, er root of the node?
I am using this to highlight the root upon clicking a child item.
private void TreeViewDecode_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
TreeViewItem treeViewItem = GetSexyGrandDaddy(e.OriginalSource as DependencyObject);
...
}
private static TreeViewItem GetSexyGrandDaddy(DependencyObject source)
{
if (source != null && source is TreeViewItem)
{
TreeViewItem root = VisualTreeHelper.GetParent(source) as TreeViewItem;
while (root.Parent != null)
{
root = root.Parent as TreeViewItem;
}
return root as TreeViewItem;
}
return null;
}
Edit: Code updated still doesn’t work
with root you mean the ‘Parent’ i guess,
look at TreeViewItem.Parent
UPDATE
look at this code:
use
instead of