How to extend WPF hit testing zone for a Path object How to extend WPF hit testing zone for a Path object wpf wpf

How to extend WPF hit testing zone for a Path object


Path.Data is a geometry object. The Geometry class has several methods that can help you hit test with tolerance:

GetFlattenedPathGeometry(Double, ToleranceType)GetOutlinedPathGeometry(Double, ToleranceType)GetRenderBounds(Pen, Double, ToleranceType)

I think GetRenderBounds will work best for you.

Once you have the geometry (plus a little width) you can call

geometry.FillContains(Point, Double, ToleranceType)

or

geometry.StrokeContains(Pen, Point, Double, ToleranceType)

Out of all of that you should tune the desired hit from your hit test;


You can wrap the Path inside a transparent Border.


In WPF you can create another path with its geometry databound to the first (using Element Binding), but with transparent brush and increased thickness.

Something more or less like this:

<Path x:Name="backPath" Data="{Binding Data, ElementName=mainPath}" StrokeThickness="10" Stroke="Transparent"/><Path x:Name="mainPath" Data="{Binding DataFromViewModel}" StrokeThickness="1" Stroke="Red"/>

Note that the main path comes after in XAML, so that it is rendered on top.