Export (0) Print
Expand All

VisualTreeHelper.HitTest Method (Visual, Point)

Returns the topmost Visual object of a hit test by specifying a Point.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public static HitTestResult HitTest(
	Visual reference,
	Point point
)
You cannot use methods in XAML.

Parameters

reference
Type: System.Windows.Media.Visual

The Visual to hit test.

point
Type: System.Windows.Point

The point value to hit test against.

Return Value

Type: System.Windows.Media.HitTestResult
The hit test result of the Visual, returned as a HitTestResult type.

The VisualHit property of the return value represents the Visual object that was hit.

NoteNote:

It is possible for a hit test that starts in a 2D visual tree to return RayHitTestResult, which is a 3D hit test result object. Hit testing that starts in a 2D visual tree seamlessly extends into any defined 3D visual tree.

The following example shows how to use the HitTest method to hit test the objects within a Canvas.

// Respond to the left mouse button down event by initiating the hit test. 
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

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

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2015 Microsoft