WPF - Determining if Mouse Is Over a UIElement
Asked Answered
V

3

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?

Valerlan answered 3/12, 2010 at 16:26 Comment(3)
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.Frog
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.Valerlan
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.Valerlan
N
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.

Nonmetallic answered 21/2, 2013 at 13:14 Comment(2)
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.Soke
What if I want exact location above a very large control?Ploughboy
S
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();
Staciastacie answered 1/2, 2012 at 9:58 Comment(0)
M
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.              
            }
}
Mcfall answered 20/6, 2022 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.