13

I have some xaml markup that looks essentially like this:

<Canvas x:Name="A">
     <Canvas x:Name="B"/>
</Canvas>

I want to determine if the mouse is over Canvas B.

When I click while my mouse is over Canvas B, Mouse.DirectlyOver returns Canvas A (as I expect). I then get a reference to Canvas B from Canvas A but when I check Canvas B's IsMouseOver property it returns false.

What is the best way to determine if the mouse is over Canvas B given the xaml above?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Brent Lamborn
  • 1,370
  • 3
  • 17
  • 37
  • What event handling are you using to do your checking? If I add a MouseUp event to Canvas B then Mouse.DirectlyOver is returning Canvas B. Perhaps a little more information would help to track down your problem. – Liz Dec 03 '10 at 17:14
  • Sure. The Canvas is actually in a ControlTemplate for a ListBoxItem. I'm handling the listbox's selection changed event, and executing a routed event to show a pop up that details information about the clicked item. The code where I check where the mouse is over is in the routed event handler. The goal of all this is to not show the pop-up when the right-side portion of the list item is clicked. So I'm attempting to define that part of the list item with a canvas, then checking to see if the mouse is over that portion of the item before showing the popup. – Brent Lamborn Dec 03 '10 at 17:36
  • I know there are other ways to do this, but I am trying to avoid having to use something other than the selection changed event. – Brent Lamborn Dec 03 '10 at 17:38

3 Answers3

39

You can use the IsMouseOver property to determine if the mouse is over a given control or not:

if(this.B.IsMouseOver)
    DoSomethingNice();

While Mouse.DirectlyOver can work, if the mouse is over a control contained by the Canvas, that control will be returned instead of the Canvas itself. IsMouseOver will work properly even in this case.

makc
  • 2,569
  • 1
  • 18
  • 28
Andy
  • 30,088
  • 6
  • 78
  • 89
  • 3
    Note that you'll have to manually detect `IsMouseOver` and `DirectlyOver` during drag & drop operations, because the mouse is captured until the drop finishes. For those that don't know, it can be done by checking if the mouse is within the control's bounds for `IsMouseOver` and using `VisualTreeHelper.HitTest` for `DirectlyOver`. – Koby Duck Dec 25 '17 at 01:26
  • What if I want exact location above a very large control? – John Demetriou Apr 05 '18 at 08:55
4

I found an answer here on SO that should help you: StackOverflow: WPF Ways to find controls

Just for reference:

I was just searching for a way to find out if my Mouse is over my applications window at all, and I successfully found this out using:

if (Mouse.DirectlyOver != null)
    DoSomethingNice();

While debugging Mouse.DirectlyOver it seemed to be that it should have found your Canvas B, as it looks for the topmost element - so your example should work. It didn't give me a dependency object, but I guess you could just compare it to your canvas using this is the codebehind (untested):

if (Mouse.DirectlyOver == this.B)
    DoSomethingNice();
Community
  • 1
  • 1
Akku
  • 4,373
  • 4
  • 48
  • 67
1

Here you can find from mouse click event with hit test.

{
            Point pt = e.GetPosition((UIElement)sender);

            // Perform the hit test against a given portion of the visual object tree.
            HitTestResult result = VisualTreeHelper.HitTest(this.B, pt);

            if (result != null)
            {
                // Perform action on hit visual object.              
            }
}
acesim88
  • 11
  • 3