在WPF中的鼠标下获取逻辑UIElement [英] Getting the Logical UIElement under the mouse in WPF

查看:183
本文介绍了在WPF中的鼠标下获取逻辑UIElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎在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.DirectlyOverVisualTreeHelper.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屋!

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