VisualTreeHelper.HitTest Method (Visual, HitTestFilterCallback, HitTestResultCallback, HitTestParameters)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Shared Sub HitTest (
	reference As Visual,
	filterCallback As HitTestFilterCallback,
	resultCallback As HitTestResultCallback,
	hitTestParameters As HitTestParameters


Type: System.Windows.Media.Visual

The Visual to hit test.

Type: System.Windows.Media.HitTestFilterCallback

The method that represents the hit test filter callback value.

Type: System.Windows.Media.HitTestResultCallback

The method that represents the hit test result callback value.

Type: System.Windows.Media.HitTestParameters

The parameter value to hit test against.

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.


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

	' 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 Function MyHitTestResult(ByVal result As HitTestResult) As HitTestResultBehavior
	' Add the hit test result to the list that will be processed after the enumeration.

	' Set the behavior to return visuals at all z-order levels.
	Return HitTestResultBehavior.Continue
End Function

.NET Framework
Available since 3.0
Return to top