VisualTreeHelper.HitTest Method

Definition

Returns the topmost Visual object of a hit test.

Overloads

HitTest(Visual, Point)

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

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

Initiates a hit test on the specified Visual3D, with caller-defined HitTestFilterCallback and HitTestResultCallback methods.

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

Initiates a hit test on the specified Visual, with caller-defined HitTestFilterCallback and HitTestResultCallback methods.

Remarks

The overloaded methods that take HitTestResultCallback and HitTestFilterCallback as parameters provide additional functionality during hit testing:

  • Allow you to participate in controlling the visual tree traversal during hit testing.

  • Allow you to retrieve all of the visuals under the point or geometry, not just the topmost one.

HitTest(Visual, Point)

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

C#
public static System.Windows.Media.HitTestResult HitTest(System.Windows.Media.Visual reference, System.Windows.Point point);

Parameters

reference
Visual

The Visual to hit test.

point
Point

The point value to hit test against.

Returns

The hit test result of the Visual, returned as a HitTestResult type.

Examples

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

C#
// 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.
    }
}

Remarks

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

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

Initiates a hit test on the specified Visual3D, with caller-defined HitTestFilterCallback and HitTestResultCallback methods.

C#
public static void HitTest(System.Windows.Media.Media3D.Visual3D reference, System.Windows.Media.HitTestFilterCallback filterCallback, System.Windows.Media.HitTestResultCallback resultCallback, System.Windows.Media.Media3D.HitTestParameters3D hitTestParameters);

Parameters

reference
Visual3D

The Visual3D to hit test.

filterCallback
HitTestFilterCallback

The method that represents the hit test filter callback value.

resultCallback
HitTestResultCallback

The method that represents the hit test result callback value.

hitTestParameters
HitTestParameters3D

The 3D parameter value to hit test against.

Remarks

The filterCallback parameter can be null, in which case, it is ignored. If filterCallback is not null, it is invoked before resultCallback. resultCallback cannot be null.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

Initiates a hit test on the specified Visual, with caller-defined HitTestFilterCallback and HitTestResultCallback methods.

C#
public static void HitTest(System.Windows.Media.Visual reference, System.Windows.Media.HitTestFilterCallback filterCallback, System.Windows.Media.HitTestResultCallback resultCallback, System.Windows.Media.HitTestParameters hitTestParameters);

Parameters

reference
Visual

The Visual to hit test.

filterCallback
HitTestFilterCallback

The method that represents the hit test filter callback value.

resultCallback
HitTestResultCallback

The method that represents the hit test result callback value.

hitTestParameters
HitTestParameters

The parameter value to hit test against.

Examples

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

C#
// Respond to the right mouse button down event by setting up a hit test results callback.
private void OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Clear the contents of the list used for hit test results.
    hitResultsList.Clear();

    // Set up a callback to receive the hit test result enumeration.
    VisualTreeHelper.HitTest(myCanvas, null,
        new HitTestResultCallback(MyHitTestResult),
        new PointHitTestParameters(pt));

    // Perform actions on the hit test results list.
    if (hitResultsList.Count > 0)
    {
        Console.WriteLine("Number of Visuals Hit: " + hitResultsList.Count);
    }
}
C#
// 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;
}

Remarks

The filterCallback parameter can be null, in which case it is ignored. If filterCallback is not null, it is invoked before resultCallback. resultCallback cannot be null.

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9