How to determine if the mouse cursor is over a given uielement?

I need to determine if the mouse cursor is over a given one UIElement. The method should work even if another element is located above it (because it has a larger Zindex).

I tried using MouseEnter / Leave events, but mouseenter does not fire if the item is not the largest item.

Any ideas?

+3
source share
1 answer

You can use the class VisualTreeHelperfor this function.

 void MouseMove(object sender, MouseEventArgs e)
 {
      Point p = e.GetPosition((UIElement)sender);
      var elems = VisualTreeHelper.FindElementsInHostCoordinates(p, (UIElement)sender)
      if (elems.Contains(theUIElementIamLookingFor))
      {
         //element is somewhere under the mouse
      }
 }
+9
source

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


All Articles