Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsMouseOver when mouse captured

Tags:

wpf

I've got an IsMouseOver trigger on my element. I've also got a drag action happening, whereby another element captures the mouse, and thus the IsMouseOver trigger never happens, yet I explicitly want it to happen on certain elements when I drag over it (mouse captured and all). Is this possible?

like image 657
George R Avatar asked Apr 26 '12 12:04

George R


1 Answers

I know this was asked years ago, but just in case someone lands here from a search engine (just like me), here is how I solved the problem for myself. Instead of using IsMouseOver property, use hit-testing in your code to determine if mouse is inside your control:

bool IsMouseOverEx = false;

VisualTreeHelper.HitTest(this, d =>
{
  if (d == this) 
  {
    IsMouseOverEx = true;
    return HitTestFilterBehavior.Stop;
  } 
  else 
   return HitTestFilterBehavior.Continue;
}, 
ht => HitTestResultBehavior.Stop, 
new PointHitTestParameters(Mouse.GetPosition(this)));

if (IsMouseOverEx) 
{
  //Do whatever you need in case of MouseOver
}

N.B. If you haven't read the question, note that this method is a workaround for situations where the mouse is "captured" and therefore IsMouseOver property is not working correctly. In normal situations, you should always use IsMouseOver.

like image 67
dotNET Avatar answered Nov 04 '22 03:11

dotNET