How can I make IsMouseOver not care about other things covering up the control?

201 views Asked by At

I use the IsMouseOver property frequently in my WPF program. Unfortunately, I have other visual elements from other branches of the visual tree that sometimes cover up my controls. When they cover up my controls, the IsMouseOver becomes false on my controls, even when my mouse is over the covered up control.

How can my control have a property like IsMouseOver that doesn't care if it's covered up at the cursor location? Like a hit test that can tunnel threw covers.

I looked at this solution but I don't think I can use it, because it basically uses polling every time that the mouse moves. I think that would slow down my program significantly, because my visual tree is extensive.

Note that the visual elements that cover up my controls are not children of my controls.

Edit: Setting IsHitTestVisible to false on the element that is doing the covering would be a very nice solution, but when I do that then the covering element doesn't interact with the mouse at all. I need both the covered and the covering elements to react to the mouse.

1

There are 1 answers

1
Fede On

Make the covering control invisible to Hit Testing by setting IsHitTestVisible="False"

<Grid>
  <Control1/>

  <!-- Control2 will visually "cover" Control1, but mouse events will "pass thru" To Control1 -->
  <Control2 IsHitTestVisible="False"/>
</Grid>