reviewMouseDown事件中获取TreeViewItem

reviewMouseDown事件中获取TreeViewItem

本文介绍了WPF TreeView,在PreviewMouseDown事件中获取TreeViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定在PreviewMouseDown事件中单击的TreeViewItem?

How can I determine TreeViewItem clicked in PreviewMouseDown event?

推荐答案

以下似乎有效:

private void myTreeView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  TreeViewItem item = GetTreeViewItemClicked((FrameworkElement)e.OriginalSource,
                                                                       myTreeView);
  ...
}

private TreeViewItem GetTreeViewItemClicked(FrameworkElement sender, TreeView treeView)
{
  Point p = ((sender as FrameworkElement)).TranslatePoint(new Point(0, 0), treeView);
  DependencyObject obj = treeView.InputHitTest(p) as DependencyObject;
  while (obj != null && !(obj is TreeViewItem))
    obj = VisualTreeHelper.GetParent(obj);
  return obj as TreeViewItem;
}

这篇关于WPF TreeView,在PreviewMouseDown事件中获取TreeViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:15