不过说一下,我不是用7楼的方法判断是否点击在TreeViewItem的,我的代码是
//Set the selected node
private void QueryTree_MouseDown(object sender, MouseButtonEventArgs e)
{
TreeView tv = sender as TreeView;
Point p = e.GetPosition(tv);
Depe…
[/Quote]
不过说一下,我不是用7楼的方法判断是否点击在TreeViewItem的,我的代码是
//Set the selected node
private void QueryTree_MouseDown(object sender, MouseButtonEventArgs e)
{
TreeView tv = sender as TreeView;
Point p = e.GetPosition(tv);
DependencyObject obj = tv.InputHitTest(p) as DependencyObject;
tviNodeOfMousedown = Utils.Utils.GetDependencyObjectFromVisualTree(obj, typeof(TreeViewItem)) as TreeViewItem;
if (tviNodeOfMousedown == null) return;
}
private DependencyObject GetDependencyObjectFromVisualTree(DependencyObject startObject, Type type)
{
//Iterate the visual tree to get the parent(ItemsControl) of this control
DependencyObject parent = startObject;
while (parent != null)
{
if (type.IsInstanceOfType(parent))
break;
else
parent = VisualTreeHelper.GetParent(parent);
}
return parent;
}