开发者

WPF TreeView, get TreeViewItem in PreviewMouseDown event

开发者 https://www.devze.com 2023-01-01 08:55 出处:网络
How can I determine TreeViewItem clicked in PreviewMouseDown event?开发者_高级运维The following seems to work:

How can I determine TreeViewItem clicked in PreviewMouseDown event?开发者_高级运维


The following seems to work:

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;
}


I originally used an extension method on TreeView that takes a UIElement--the sender of the PreviewMouseDown event--like this:

private void MyTreeView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    var uiElement = sender as UIElement;
    var treeViewItem = myTreeView.TreeViewItemFromChild(uiElement);
}

Here's the extension method (it checks the child itself in case you clicked right on a TreeViewItem directly)...

public static TreeViewItem TreeViewItemFromChild(this TreeView treeView, UIElement child)
{
    UIElement proposedElement = child;

    while ((proposedElement != null) && !(proposedElement is TreeViewItem))
        proposedElement = VisualTreeHelper.GetParent(proposedElement) as UIElement;

    return proposedElement as TreeViewItem;
}

Update:

However, I've since switched it to a more generic version that I can use anywhere.

public static TAncestor FindAncestor<TAncestor>(this UIElement uiElement)
{
    while ((uiElement != null) && !(uiElement is TAncestor))
        retVal = VisualTreeHelper.GetParent(uiElement) as UIElement;

    return uiElement as TAncestor;
}

That either finds the type you're looking for (again, including checking itself) or returns null

You'd use it in the same PreviewMouseDown handler like so...

private void MyTreeView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    var uiElement = sender as UIElement;
    var treeViewItem = uiElement.FindAncestor<TreeViewItem>();
}

This came in very handy for when my TreeViewItem had a CheckBox in its template and I wanted to select the item when the user clicked the checkbox which normally swallows the event.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号