Skip to main content
HitTestFilterCallback Delegate
 

Represents the callback method that specifies parts of the visual tree to omit from hit test processing

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

Syntax
public delegate HitTestFilterBehavior HitTestFilterCallback(
	DependencyObject potentialHitTestTarget
)
public delegate HitTestFilterBehavior HitTestFilterCallback(
	DependencyObject^ potentialHitTestTarget
)
type HitTestFilterCallback = 
    delegate of 
        potentialHitTestTarget:DependencyObject -> HitTestFilterBehavior
Public Delegate Function HitTestFilterCallback (
	potentialHitTestTarget As DependencyObject
) As HitTestFilterBehavior

Parameters

potentialHitTestTarget
Type:

The visual to hit test.

Return Value

Type:

A HitTestFilterBehavior that represents the action resulting from the hit test.

Remarks

The hit test filter callback method is invoked for all the visual objects that map to the hit test criteria, starting from the visual you specify and descending through its branch of the visual tree. However, you may want to ignore certain branches of the visual tree that you are not interested in processing in your hit test results callback function. The return value of the hit test filter callback function determines what type of action the enumeration of the visual objects should take. For example, if you return the value, ContinueSkipSelfAndChildren, you can remove the current visual object and its descendants from the hit test results enumeration. This means that the hit test results callback method will not see these objects in its enumeration.

System_CAPS_noteNote

Pruning the visual tree of objects decreases the amount of processing during the hit test results enumeration pass.

Pruning a visual tree using a hit test filter

Pruning a visual tree

Examples

The following example shows how to invoke HitTest by using a HitTestFilterCallback value. The corresponding hit test callback method is also defined.

// Respond to the mouse wheel event by setting up a hit test filter and results enumeration.
private void OnMouseWheel(object sender, MouseWheelEventArgs 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,
                      new HitTestFilterCallback(MyHitTestFilter),
                      new HitTestResultCallback(MyHitTestResult),
                      new PointHitTestParameters(pt));

    // Perform actions on the hit test results list.
    if (hitResultsList.Count > 0)
    {
        ProcessHitTestResultsList();
    }
}
' Respond to the mouse wheel event by setting up a hit test filter and results enumeration.
Private Overloads Sub OnMouseWheel(ByVal sender As Object, ByVal e As MouseWheelEventArgs)
	' 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, New HitTestFilterCallback(AddressOf MyHitTestFilter), New HitTestResultCallback(AddressOf MyHitTestResult), New PointHitTestParameters(pt))

	' Perform actions on the hit test results list.
	If hitResultsList.Count > 0 Then
		ProcessHitTestResultsList()
	End If
End Sub

The following example shows how to return a HitTestFilterBehavior value from a hit test filter callback method.

// Filter the hit test values for each object in the enumeration.
public HitTestFilterBehavior MyHitTestFilter(DependencyObject o)
{
    // Test for the object value you want to filter.
    if (o.GetType() == typeof(Label))
    {
        // Visual object and descendants are NOT part of hit test results enumeration.
        return HitTestFilterBehavior.ContinueSkipSelfAndChildren;
    }
    else
    {
        // Visual object is part of hit test results enumeration.
        return HitTestFilterBehavior.Continue;
    }
}
' Filter the hit test values for each object in the enumeration.
Public Function MyHitTestFilter(ByVal o As DependencyObject) As HitTestFilterBehavior
	' Test for the object value you want to filter.
	If o.GetType() Is GetType(Label) Then
		' Visual object and descendants are NOT part of hit test results enumeration.
		Return HitTestFilterBehavior.ContinueSkipSelfAndChildren
	Else
		' Visual object is part of hit test results enumeration.
		Return HitTestFilterBehavior.Continue
	End If
End Function
Version Information
.NET Framework
Available since 3.0
Return to top