wpf - 如何在 XAML 中运行时动态更改 PATH DATA 属性?

标签 wpf xaml mvvm pathgeometry

我有一个带有自定义 ControlTemplate 的按钮。在执行过程中,我想根据用户选择的条件更改 PathGeometry 和 Fill。

在 ResourceDictionary 中,我有两个 PathGeometry 定义:

<PathGeometry x:Key="Card" >
    <PathFigure StartPoint="0,0" >
        <LineSegment Point="0,50"/>
        <LineSegment Point="100,50"/>
        <LineSegment Point="100,20"/>
        <LineSegment Point="80,0"/>
        <LineSegment Point="0,0"/>
    </PathFigure>
</PathGeometry>

<PathGeometry x:Key="Triangle" >
    <PathFigure StartPoint="0,0" >
        <LineSegment Point="50,50"/>
        <LineSegment Point="100,0"/>
        <LineSegment Point="0,0"/>
    </PathFigure>
</PathGeometry>

在同一个 ResourceDictionary 中,我定义了一个样式:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="grid"  Background="White">
                    <Path Stroke="Black" StrokeThickness="2" Data="{DynamicResource Card}"> 
                        <Path.Style>
                            <Style TargetType="Path">
                                <Style.Triggers>
                                    <DataTrigger
                                        Binding="{Binding HatchType, 
                                        RelativeSource={RelativeSource FindAncestor, AncestorType=local:PullArrowConnector}, 
                                        Mode=TwoWay}" Value="Withdrawal">
                                        <Setter Property="Fill" Value="{DynamicResource HatchBrush}" />
                                    </DataTrigger>
                                    <DataTrigger
                                        Binding="{Binding HatchType, 
                                        RelativeSource={RelativeSource FindAncestor, AncestorType=local:PullArrowConnector}, 
                                        Mode=TwoWay}" Value="Production">
                                        <Setter Property="Fill" Value="Transparent" />
                                    </DataTrigger>
                                    <DataTrigger
                                        Binding="{Binding HatchType, 
                                        RelativeSource={RelativeSource FindAncestor, AncestorType=local:PullArrowConnector}, 
                                        Mode=TwoWay}" Value="Signal">
                                        <Setter Property="Path.Data" Value="{DynamicResource Triangle}" />
                                        <Setter Property="Fill" Value="Transparent" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Path.Style>
                    </Path>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用DataTrigger,我可以动态控制Fill 属性,但不能动态控制Data 属性。我知道 Data 不是 Path.Style 的属性,但我不清楚如何在运行时动态更改 PathGeometry 。上面显示的第三个 DataTrigger,我尝试设置 Path.Data 属性不起作用。

最佳答案

不能直接设置Path的Data属性,因为它具有更高的value precedence比来自样式 setter 的值。因此,DataTrigger 的 Setter 会被忽略。

添加另一个 Setter:

<Path Stroke="Black" StrokeThickness="2"> 
    <Path.Style>
        <Style TargetType="Path">
            <Setter Property="Data" Value="{DynamicResource Card}"/>
            <Style.Triggers>
                <DataTrigger ...>
                    <Setter Property="Data" Value="{DynamicResource Triangle}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    <Path.Style>
</Path>

关于wpf - 如何在 XAML 中运行时动态更改 PATH DATA 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49132771/

相关文章:

wpf - 如何显示绑定(bind)数据网格的值

c# - 当 ListView 的 ItemsSource 改变时触发事件

c# - 将按钮控件嵌入到现有的 Direct3D 应用程序中

c# - MainWindow 打开两次(StartupUri、App、Loadscreen)

wpf - 实例化其他 XAML 文件中定义的对象

wpf - 将数据传递给 mvvm 用户控件

wpf - 了解路由事件 : Why I need both, 气泡和隧道事件?

windows - 在 Windows 8 上开发应用程序的最低硬件和软件要求?

c# - Xamarin Forms 两种方式绑定(bind)ActivityIndi​​cator

c# - 使用ViewModel公开嵌套模型