3

I need to determine whether the mouse cursor is over a given UIElement. The method should work even if another element is placed on top of it (because it has a bigger Zindex).

I tried using MouseEnter/Leave events but mouseenter does not trigger if the element is not the top most element.

Any ideas?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
clems
  • 31
  • 1
  • 2

1 Answers1

9

You can use the VisualTreeHelper class for 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
      }
 }
ChrisF
  • 134,786
  • 31
  • 255
  • 325
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306