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

问题描述 投票:2回答:1

我有一个带有自定义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:

<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。我尝试设置Path.Data属性的上面显示的第三个DataTrigger不起作用。

wpf xaml mvvm pathgeometry
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.