4

我对 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>

我似乎无法让路径数据显示在按钮上。我错过了什么?

4

1 回答 1

2

您的问题是您正在创建 type 的附加属性,Path但将其分配给PathData模板中的属性,该属性是 type Geometry。要解决此问题,请更改附加的属性声明,如下所示:

Public Class CustomShapeButton
Public Shared PathDataProperty As DependencyProperty = DependencyProperty.RegisterAttached("PathData", GetType(Geometry), GetType(CustomShapeButton), New PropertyMetadata(Nothing))

Public Shared Sub SetPathData(obj As DependencyObject, value As Geometry)
    obj.SetValue(PathDataProperty, value)
End Sub
Public Shared Function GetPathData(obj As DependencyObject) As Geometry
    Return DirectCast(obj.GetValue(PathDataProperty), Geometry)
End Function
End Class

然后像这样使用它:

<Button Content="Button" Width="60" Height="50"
        hmi:CustomShapeButton.PathData="M1 1 L20 0 L20 20 L0 20 Z"
        Style="{StaticResource TransparentNavButton}" />

还有一点需要注意:你的CustomShapeButton类不需要继承自Button——事实上,它根本不需要从任何东西继承。但是,如果您想创建自己的具有该PathData属性的派生按钮类,那么您可以继承 from Button,在这种情况下,您将声明PathData为常规依赖属性而不是附加属性。

于 2013-10-12T20:21:54.477 回答