VisualTreeHelper.HitTest VisualTreeHelper.HitTest VisualTreeHelper.HitTest VisualTreeHelper.HitTest Method

Definition

Returns the topmost Visual object of a hit test.

Overloads

HitTest(Visual, Point) HitTest(Visual, Point) HitTest(Visual, Point)

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

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) 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) HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters) 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) HitTest(Visual, Point) HitTest(Visual, Point)

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

public:
 static System::Windows::Media::HitTestResult ^ HitTest(System::Windows::Media::Visual ^ reference, System::Windows::Point point);
public static System.Windows.Media.HitTestResult HitTest (System.Windows.Media.Visual reference, System.Windows.Point point);
static member HitTest : System.Windows.Media.Visual * System.Windows.Point -> System.Windows.Media.HitTestResult

Parameters

reference
Visual Visual Visual Visual

The Visual to hit test.

point
Point Point 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.

// 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.
    }
}
' Respond to the left mouse button down event by initiating the hit test.
Private Overloads Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
	' Retrieve the coordinate of the mouse position.
	Dim pt As Point = e.GetPosition(CType(sender, UIElement))

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

	If result IsNot Nothing Then
		' Perform action on hit visual object.
	End If
End Sub

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

HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D) HitTest(Visual3D, HitTestFilterCallback, HitTestResultCallback, HitTestParameters3D)

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

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);
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);
static member HitTest : System.Windows.Media.Media3D.Visual3D * System.Windows.Media.HitTestFilterCallback * System.Windows.Media.HitTestResultCallback * System.Windows.Media.Media3D.HitTestParameters3D -> unit
Public Shared Sub HitTest (reference As Visual3D, filterCallback As HitTestFilterCallback, resultCallback As HitTestResultCallback, hitTestParameters As HitTestParameters3D)

Parameters

reference
Visual3D Visual3D Visual3D Visual3D

The Visual3D to hit test.

filterCallback
HitTestFilterCallback HitTestFilterCallback HitTestFilterCallback HitTestFilterCallback

The method that represents the hit test filter callback value.

resultCallback
HitTestResultCallback HitTestResultCallback HitTestResultCallback HitTestResultCallback

The method that represents the hit test result callback value.

hitTestParameters
HitTestParameters3D HitTestParameters3D HitTestParameters3D 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.

HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters) HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters) HitTest(Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)

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

public:
 static void HitTest(System::Windows::Media::Visual ^ reference, System::Windows::Media::HitTestFilterCallback ^ filterCallback, System::Windows::Media::HitTestResultCallback ^ resultCallback, System::Windows::Media::HitTestParameters ^ hitTestParameters);
public static void HitTest (System.Windows.Media.Visual reference, System.Windows.Media.HitTestFilterCallback filterCallback, System.Windows.Media.HitTestResultCallback resultCallback, System.Windows.Media.HitTestParameters hitTestParameters);
static member HitTest : System.Windows.Media.Visual * System.Windows.Media.HitTestFilterCallback * System.Windows.Media.HitTestResultCallback * System.Windows.Media.HitTestParameters -> unit

Parameters

reference
Visual Visual Visual Visual

The Visual to hit test.

filterCallback
HitTestFilterCallback HitTestFilterCallback HitTestFilterCallback HitTestFilterCallback

The method that represents the hit test filter callback value.

resultCallback
HitTestResultCallback HitTestResultCallback HitTestResultCallback HitTestResultCallback

The method that represents the hit test result callback value.

hitTestParameters
HitTestParameters HitTestParameters 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.

// 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);
    }
}
' Respond to the right mouse button down event by setting up a hit test results callback.
Private Overloads Sub OnMouseRightButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
	' Retrieve the coordinate of the mouse position.
	Dim pt As Point = e.GetPosition(CType(sender, UIElement))

	' 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, Nothing, New HitTestResultCallback(AddressOf MyHitTestResult), New PointHitTestParameters(pt))

	' Perform actions on the hit test results list.
	If hitResultsList.Count > 0 Then
		Console.WriteLine("Number of Visuals Hit: " & hitResultsList.Count)
	End If
End Sub
// 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;
}
' Return the result of the hit test to the callback.
Public Function MyHitTestResult(ByVal result As HitTestResult) As HitTestResultBehavior
	' 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
End Function

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