【问题标题】:WPF: Making the entire "block" of a path clickableWPF:使路径的整个“块”可点击
【发布时间】:2011-01-11 06:09:15
【问题描述】:

我的一些Buttons 有一个特殊的ControlTemplate

<ControlTemplate TargetType="{x:Type Button}">
    <Path Name="ThePath" Fill="White" Stretch="UniformToFill" 
          Width="12" Height="12" Stroke="White"
          StrokeThickness="4" 
          Data="M1.5,1.5 L10.5,10.5 M1.5,10.5 L10.5,1.5"/>
    <ControlTemplate.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="Fill" Value="#afa" TargetName="ThePath"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

这很好用,但由于我使用的是Path(在这种情况下,它的形状像一个胖 X),确切地说路径是可点击的,而不是X 的角落。我可以使用任何自动魔法来使 X 的整个“块”可点击吗?

我考虑过将路径包装在一个矩形对象中,但我只是想确保我没有遗漏一些琐碎的东西。

【问题讨论】:

  • 你没有遗漏任何东西,它就是这样完成的(用矩形、网格、画布或任何具有非空背景的东西。

标签: c# wpf path


【解决方案1】:

Aviad P. 是正确的。我就是这样做的:

<Border Background="Transparent">
  <Path ... />
</Border>

这是因为当“命中测试”以确定鼠标点击的路径时,“透明”画笔被认为是常规颜色。

【讨论】:

  • 附注:实际上没有必要将路径包裹任何东西,可以在模板中使用包含应该可点击形状的第二个路径并设置Fill="Transparent"(在我的情况下,我需要有一个可点击的圆形并且不想摆弄BorderCornerRadius)。
  • 这仅在我将IsHitTestVisible="True" 添加到Border 时才对我有用
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 2022-12-31
  • 2022-11-27
  • 1970-01-01
相关资源
最近更新 更多