How to dynamically change a PATH DATA property at

2019-07-23 05:37发布

问题:

I have a button with a custom ControlTemplate. During execution, I want to change the PathGeometry and Fill based on criteria the user selects.

In a ResourceDictionary I have two PathGeometry definitions:

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

and

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

In the same ResourceDictionary I have a Style defined:

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

Using a DataTrigger, I'm able to dynamically control the Fill property, but not the Data property. I understand that Data is not a property of Path.Style, but its not clear to me how I can dynamically change the PathGeometry at runtime. The third DataTrigger shown above where I try to set the Path.Data property doesn't work.

回答1:

You must not directly set the Data property of the Path, because it has higher value precedence than a value from a Style Setter. As a result, the Setter of your DataTrigger is ignored.

Add another Setter instead:

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