Getting logical UIElement under the mouse in WPF

It seems that the whole way of extracting an element under the mouse is related to testing Visual Hit.

Is there some mechanism that I am missing that will allow me to grab the actual UIElement that represents the current visual tree returned by HitTest ?

Summary of what I am doing:

I have my own hint class that relies on doing something based on the UIElement that the mouse has ended.

Simply put, it hooks into its own Window PreviewMouseMove event and updates the "current item". This current item should represent the UIElement that the mouse is currently on top of.

Unfortunately, everything I came across with Mouse.DirectlyOver , VisualTreeHelper.HitTest (included callbacks), does not work.

Can someone give an idea on how to accomplish a seemingly simple task in WPF in a Window MouseMove ?

+4
source share
2 answers

Use the Mouse.DirectlyOver property:

 var UIElement = Mouse.DirectlyOver as UIElement; 
+6
source

I don’t know of any explicit way, but the idea is quite simple,

1) Find the visual tree element

2) Check if the element is present in the logical tree,

3) If it is present, you have found the answer.

4) Otherwise, you need to call VisualTreeHelper.GetParent() and continue the algorithm.

Ps, LogicalTreeHelper.GetParent(your visual tree element) MAY also work.

+2
source

Source: https://habr.com/ru/post/1480516/


All Articles