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

时间:2018-03-06 14:07:55

标签: 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 ,我可以动态控制填充属性,但不能动态控制数据属性。我了解数据不是 Path.Style 的属性,但我不清楚如何在运行时动态更改 PathGeometry 。我尝试设置 Path.Data 属性的上面显示的第三个 DataTrigger 不起作用。

1 个答案:

答案 0 :(得分:4)

您不能直接设置Path的Data属性,因为它比Style Setter中的值高value precedence。因此,将忽略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>