Permalink
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (38 sloc) 3.61 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Hit Test Using Geometry as a Parameter
03/30/2017
.net-framework
dotnet-wpf
article
hit tests, on visual objects using Geometry objects
visual objects, hit tests on
Geometry objects, hit tests on visual objects
6c8bdbf2-19e0-4fbb-bf89-c1252b2ebc61
8
dotnet-bot
dotnetcontent
wpickett

How to: Hit Test Using Geometry as a Parameter

This example shows how to perform a hit test on a visual object using a xref:System.Windows.Media.Geometry as a hit test parameter.

Example

The following example shows how to set up a hit test using xref:System.Windows.Media.GeometryHitTestParameters for the xref:System.Windows.Media.VisualTreeHelper.HitTest%2A method. The xref:System.Windows.Point value that is passed to the OnMouseDown method is used to create a xref:System.Windows.Media.Geometry object in order to expand the range of the hit test.

[!code-csharpHitTestingOverview#HitTestingOverviewSnippet10] [!code-vbHitTestingOverview#HitTestingOverviewSnippet10]

The xref:System.Windows.Media.GeometryHitTestResult.IntersectionDetail%2A property of xref:System.Windows.Media.GeometryHitTestResult provides information about the results of a hit test that uses a xref:System.Windows.Media.Geometry as a hit test parameter. The following illustration shows the relationship between the hit test geometry (the blue circle) and the rendered content of the target visual object (the red square).

Diagram of IntersectionDetail used in hit testing
Intersection between hit test geometry and target visual object

The following example shows how to implement a hit test callback when a xref:System.Windows.Media.Geometry is used as a hit test parameter. The result parameter is cast to a xref:System.Windows.Media.GeometryHitTestResult in order to retrieve the value of the xref:System.Windows.Media.GeometryHitTestResult.IntersectionDetail%2A property. The property value allows you to determine if the xref:System.Windows.Media.Geometry hit test parameter is fully or partially contained within the rendered content of the hit test target. In this case, the sample code is only adding hit test results to the list for visuals that are fully contained within the target boundary.

[!code-csharpHitTestingOverview#HitTestingOverviewSnippet11] [!code-vbHitTestingOverview#HitTestingOverviewSnippet11]

[!NOTE] The xref:System.Windows.Media.HitTestResult callback should not be called when the intersection detail is xref:System.Windows.Media.IntersectionDetail.Empty.

See Also

Hit Testing in the Visual Layer
Hit Test Geometry in a Visual