给定一个树中的随机 TreeViewItem,我如何检索节点的祖父,呃根?
单击子项时,我正在使用它来突出显示根。
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;
}
编辑:更新的代码仍然不起作用