【问题标题】:WPF custom shaped button template using path as attached property使用路径作为附加属性的 WPF 自定义形状按钮模板
【发布时间】:2013-10-12 19:42:54
【问题描述】:

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

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

【问题讨论】:

    标签: wpf vb.net xaml button path


    【解决方案1】:

    您的问题是您正在创建Path 类型的附加属性,但将其分配给模板中PathData 类型的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 属性的派生按钮类,则可以从Button 继承,在这种情况下,您可以将PathData 声明为常规依赖属性而不是附加属性.

    【讨论】:

    • 像魅力一样工作,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多