在WPF中的鼠标下获取逻辑UIElement [英] Getting the Logical UIElement under the mouse in WPF
问题描述
似乎在Mouse下检索元素的所有方式都与Visual Hit测试有关.
It seems that all of the way of retrieving an element under the Mouse relates to Visual Hit testing.
我是否缺少某种机制,可以让我抓取表示HitTest
返回的当前可视树的实际UIElement
?
Is there some mechanism that I'm missing which would allow me to grab the actual UIElement
that represents the current visual tree that the HitTest
returns?
我在做什么摘要:
我有一个自定义的工具提示类,该类依赖于基于鼠标悬停在的UIElement上的操作.
I have a custom tooltip class which relies on doing something based on the UIElement that the mouse is over.
简单地说,它挂接到拥有的Window的PreviewMouseMove
事件中并更新当前项".当前项目应代表鼠标当前位于其上方的UIElement
.
Simply put, it hooks into the owning Window's PreviewMouseMove
event and updates a "current Item". This current item should represent the UIElement
that the mouse is currently over top of.
不幸的是,我在Mouse.DirectlyOver
和VisualTreeHelper.HitTest
上遇到的所有问题(包括回调)都无法正常工作.
Unfortunately everything I've encountered with Mouse.DirectlyOver
, VisualTreeHelper.HitTest
(callbacks included) doesn't work.
任何人都可以在Window的MouseMove
事件中提供有关如何在WPF中完成看似简单的任务的见解吗?
Can anyone offer insight in how to accomplish a seemingly simple task in WPF within Window's MouseMove
event?
推荐答案
Use the Mouse.DirectlyOver
property:
var UIElement = Mouse.DirectlyOver as UIElement;
这篇关于在WPF中的鼠标下获取逻辑UIElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!