开发者

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

开发者 https://www.devze.com 2022-12-11 03:43 出处:网络
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 h开发者_JAVA百科as a bigger Zindex).

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 h开发者_JAVA百科as 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?


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
      }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消