如何使用树型视图坐标/点获得通过HitTesting在WPF? [英] How to get TreeViewItem using a coordinate/point via HitTesting in WPF?

查看:270
本文介绍了如何使用树型视图坐标/点获得通过HitTesting在WPF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让树型视图鼠标下,但无法找到一个方法来做到这一点。

I am trying to get the TreeViewItem under the mouse, but can't find a way to do it.

目前我使用这样的:

Layer GetItemAtLocation ( Point point )
{
    HitTestResult result = VisualTreeHelper.HitTest ( this, point );
    FrameworkElement element = result.VisualHit as FrameworkElement;
    if ( element != null )
        return element.DataContext as Layer;

    return null;
}

这方法是有用的,但是这并没有给树型视图下鼠标。

This method is useful but that doesn't give the TreeViewItem under mouse.

这是如何做到这一点任何想法?

Any ideas on how to do this?

推荐答案

您没有指定什么导致你需要这个,这将有助于一点。

You don't specify what lead you to need this, which would help a little.

这是一种讨厌的,但你可以这样做:

This is kind of nasty, but you could do this:

IInputElement dropNode = tree.InputHitTest(point);

这将可能给你一个TextBlock,所以你将不得不使用VisualTreeHelper.GetParent()通过文本块,内容presenter,边框,网格,然后最终的树型视图。

This will probably give you a TextBlock, so you would then have to use VisualTreeHelper.GetParent() to navigate up through the TextBlock, ContentPresenter, Border, Grid, and then eventually the TreeViewItem.

这当然不是最好的办法,但它应该工作。

This is certainly not the nicest approach, but it should work.

根据你在做什么,迈克Hillberg的博客提供了一个附加属性,方法,可以在触发器使用。请参阅<一href="http://blogs.msdn.com/b/mikehillberg/archive/2006/09/21/mytreeviewhelperismousedirectlyoveritem.aspx"相对=nofollow>这里

Depending on what you are doing, Mike Hillberg's blog provided an attached property approach that be used in a trigger. See here

在他的榜样,他把一个树型视图绿色,当鼠标移动到它。

In his example, he is turning a TreeViewItem green when the mouse is over it.

<TreeView.Resources>
      <Style TargetType="TreeViewItem">
        <Style.Triggers>
          <Trigger Property="local:MyTreeViewHelper.IsMouseDirectlyOverItem" Value="True">
            <Setter Property="Background" Value="Green" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </TreeView.Resources>

在MyTreeViewHelper类保持的MouseEnter /鼠标离开事件的跟踪,并给出了可以在触发器和东西可以用一个不错的属性,像上面的。

The MyTreeViewHelper class keeps track of the MouseEnter/MouseLeave events, and gives a nice Property that can be used in triggers and things, like the one above.

我希望帮助。

这篇关于如何使用树型视图坐标/点获得通过HitTesting在WPF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆