我对 WPF 还是很陌生。我正在尝试创建一个使用按钮形状的路径数据的按钮模板。我的应用程序有几个按钮可以执行类似的任务,但需要不同的形状。我正在尝试创建一个附加属性,它将路径数据传递给我的模板。到目前为止我得到了什么:
附带属性:
Public Class CustomShapeButton
Inherits Button
Public Shared PathDataProperty As DependencyProperty = DependencyProperty.RegisterAttached("PathData", GetType(Path), GetType(CustomShapeButton), New PropertyMetadata(Nothing))
Public Shared Sub SetPathData(obj As DependencyObject, value As Path)
obj.SetValue(PathDataProperty, value)
End Sub
Public Shared Function GetPathData(obj As DependencyObject) As Path
Return DirectCast(obj.GetValue(PathDataProperty), Path)
End Function
End Class
还有按钮模板,位于我的资源字典中:
<Style x:Key="TransparentNavButton" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Path x:Name="pth" Data="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(hmi:CustomShapeButton.PathData)}" Stroke="Black" Stretch="Fill"/>
<ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,4,0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="pth" Property="Fill" Value="#60FF0000"/>
<Setter TargetName="pth" Property="Stroke" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后尝试像这样实现它:
<Button Content="Button" Width="60" Height="50" Style="{StaticResource TransparentNavButton}">
<hmi:CustomShapeButton.PathData>
<Path Data="M1 1 L20 0 L20 20 L0 20 Z"/>
</hmi:CustomShapeButton.PathData>
</Button>
我似乎无法让路径数据显示在按钮上。我错过了什么?