37

Is there a way with WPF to get an array of elements under the mouse on a MouseMove event?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
NotDan
  • 31,709
  • 36
  • 116
  • 156

3 Answers3

47

You can also try using the Mouse.DirectlyOver property to get the top-most element that is under the mouse.

Andy
  • 30,088
  • 6
  • 78
  • 89
  • This will **not** provide an array of elements under the mouse, though, but indeed only return the topmost element. – O. R. Mapper Aug 21 '13 at 08:13
  • for me this returns null when used in a ListControl. weird. – Martien de Jong Jan 08 '19 at 11:50
  • 1
    one note - doesn't work with `Mouse.Capture(...)` and `Mouse.PreviewMouseDownOutsideCapturedElementEvent` : `If an element has mouse capture, the mouse pointer is considered directly over the element regardless of the where the mouse pointer is.` – aderesh Jan 31 '19 at 14:29
  • `Mouse.Capture(...)` and `Mouse.DirectlyOver` related question - https://stackoverflow.com/questions/5399357/how-can-i-get-the-element-at-the-mouse-position-even-when-the-mouse-is-captured?lq=1 – aderesh Jan 31 '19 at 14:32
40

From "WPF Unleashed", page 383:

Visual hit testing can inform you about all Visuals that intersect a location, [...] you must use [...] the [VisualTreeHelper.]HitTest method that accepts a HitTestResultCallback delegate. Before this version of HitTest returns, the delegate is invoked once for each relevant Visual, starting from the topmost and ending at the bottommost.

The signature of such a callback is

HitTestResultBehavior Callback(HitTestResult result)

and it has to return HitTestResultBehaviour.Continue to receive further hits, as shown below (from the linked page on MSDN):

// Return the result of the hit test to the callback.
public HitTestResultBehavior MyHitTestResult(HitTestResult result)
{
    // Add the hit test result to the list that will be processed after the enumeration.
    hitResultsList.Add(result.VisualHit);

    // Set the behavior to return visuals at all z-order levels.
    return HitTestResultBehavior.Continue;
}

For further information, please consult the MSDN documentation for VisualTreeHelper.HitTest.

thatguy
  • 21,059
  • 6
  • 30
  • 40
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
  • 6
    See the following msdn link (section: Using a Hit Test Result Callback) for a good example. [http://msdn.microsoft.com/en-us/library/ms752097.aspx](http://msdn.microsoft.com/en-us/library/ms752097.aspx) – Mike Fuchs Jan 24 '13 at 10:27
  • 1
    Note that you get a visual tree element which most of the time is not what you need. You have to search up through the parents of the hits recursively to get your logical view elements. – henon Aug 13 '15 at 13:19
4

Can you use the VisualTreeHelper.HitTest ?

http://lukieb.blogspot.com/2008/07/visualtreehelperhittest.html

Espo
  • 41,399
  • 21
  • 132
  • 159
  • The overload used in the linked example ([which should probably be replicated directly in the answer, as well](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers)) will only find the topmost element at the mouse location, though, **not** an array of elements under the mouse. – O. R. Mapper Aug 21 '13 at 08:15