WPF - Polyline / Path周围的HitTest半径

时间:2017-08-14 08:55:54

标签: c# .net wpf

我在画布上有一条很宽的1px宽线,我希望线周围的区域(例如5像素宽)可以测试。

<Polyline Points="10,10,50,50,90,10,130,50,170,10,210,50,250,10" StrokeThickness="1">
  <Polyline.Style>
    <Style TargetType="Polyline">
      <Setter Property="Stroke" Value="Magenta"/>
      <Setter Property="StrokeThickness" Value="1"/>
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Stroke" Value="Blue" />
        </Trigger>
    </Style.Triggers>
    </Style>
  </Polyline.Style>
</Polyline>

上面的示例代码难以命中折线 我不想在折线周围添加Border(使线内的所有区域都可以触及) 设置Fill属性不适用于直线,并使该行内的(某些)区域可命中...

1 个答案:

答案 0 :(得分:1)

不是最好但工作正常的解决方案。使用较大的StrokeThickness值和Transparent Stroke颜色创建折线的副本。在原始Polyline中添加DataTrigger以捕获新隐形IsMouseOver的{​​{1}}属性更改

Polyline

请注意,不应删除原始<Canvas> <Polyline x:Name="HitTestPolyline" Points="10,10,50,50,90,10,130,50,170,10,210,50,250,10" Stroke="Transparent" StrokeThickness="5"/> <Polyline Points="10,10,50,50,90,10,130,50,170,10,210,50,250,10" StrokeThickness="1"> <Polyline.Style> <Style TargetType="Polyline"> <Setter Property="Stroke" Value="Magenta"/> <Setter Property="StrokeThickness" Value="1"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=HitTestPolyline, Path=IsMouseOver}" Value="True"> <Setter Property="Stroke" Value="Blue" /> </DataTrigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Stroke" Value="Blue" /> </Trigger> </Style.Triggers> </Style> </Polyline.Style> </Polyline> </Canvas>