Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the Logical UIElement under the mouse in WPF

It seems that all of the way of retrieving an element under the Mouse relates to Visual Hit testing.

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?

Summary of what I'm doing:

I have a custom tooltip class which relies on doing something based on the UIElement that the mouse is over.

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.

Unfortunately everything I've encountered with Mouse.DirectlyOver, VisualTreeHelper.HitTest (callbacks included) doesn't work.

Can anyone offer insight in how to accomplish a seemingly simple task in WPF within Window's MouseMove event?

like image 979
tronious Avatar asked Feb 16 '23 13:02

tronious


1 Answers

Use the Mouse.DirectlyOver property:

var UIElement = Mouse.DirectlyOver as UIElement;
like image 53
Federico Berasategui Avatar answered Feb 28 '23 17:02

Federico Berasategui