如何在运行时在 XAML 中动态更改 PATH DATA 属性? [英] How to dynamically change a PATH DATA property at runtime in XAML?

查看:41
本文介绍了如何在运行时在 XAML 中动态更改 PATH DATA 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>

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

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

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>

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

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.

推荐答案

不能直接设置Path的Data属性,因为它有更高的值优先 比来自样式设置器的值.因此,您的 DataTrigger 的 Setter 将被忽略.

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.

改为添加另一个 Setter:

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>

这篇关于如何在运行时在 XAML 中动态更改 PATH DATA 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆