首页 > 解决方案 > Wpf Xaml 更改画布属性“Path.Fill”

问题描述

我有一个场景。我有我在单独的 xaml 文件 MyLogo.xaml 中定义的自定义矢量徽标。我还添加了默认填充颜色。

现在当我运行我的应用程序时,我想根据枚举值更改它的颜色,但不知何故它不会改变......有人可以帮我吗?

这是我的徽标和触发器声明的 xaml:

<misc:MyLogo
    DockPanel.Dock="Top"
    Height="200"
    Margin="0 30 0 0">
    <misc:MyLogo.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyEnum}" Value="{x:Static enum:MyEnum.ValueOne}">
                    <Setter Property="Path.Fill" Value="#FFFFFF" />
                </DataTrigger>
                <DataTrigger Binding="{Binding MyEnum}" Value="{x:Static enum:MyEnum.ValueTwo}">
                    <Setter Property="Path.Fill" Value="#000000" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </misc:MyLogo.Style>
</misc:MyLogo>

如果有帮助,我正在使用 Caliburn Micro。那么请你告诉我错误在哪里,或者有什么样的解决方法?

我尝试了如何在 XAML解决方案中使用触发器更改路径填充(在按钮上),但它没有帮助......

更新 1

MyLogo.xaml

如您所见, Path 具有默认的 Fill 值:

<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stretch="Uniform">
    <Canvas Name="svg4" Width="10" Height="9">
        <Canvas.RenderTransform>
            <TranslateTransform X="0" Y="0"/>
        </Canvas.RenderTransform>
        <Canvas.Resources/>
        <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path2" Fill="#FFFFFFFF">
            <Path.Data>
                <PathGeometry Figures="M10 4.194v.612H1.195L5.049 8.57l-.44.43L0 4.5 4.608 0l.441.43-3.854 3.764z" FillRule="EvenOdd"/>
            </Path.Data>
        </Path>
    </Canvas>
</Viewbox>

更新2(问题答案) 经过一些研究,我终于设法解决了我的问题......所以如果有人会面对这个,最简单的解决方案是:

  1. 正如在评论中一些用户写给我的那样,我应该从我的 xaml 中删除 Viewbox 和 Canvas 元素,然后你可以离开它(在我的场景中,它是从 svg 自动生成的 xaml 文件,如果它是生成的,那么它应该是有效的)

  2. 要更改徽标的颜色,我必须为我的 xaml 创建依赖属性。它看起来像这样:

    public partial class MyLogo: UserControl
    {
        public static readonly DependencyProperty BackgroundFillProperty =
            DependencyProperty.Register("BackgroundFill", typeof(SolidColorBrush), typeof(MyLogo), new PropertyMetadata(new SolidColorBrush()));
    
    public MyLogo() => InitializeComponent();
    
    public SolidColorBrush BackgroundFill
    {
        get => (SolidColorBrush)GetValue(BackgroundFillProperty);
        set => SetValue(BackgroundFillProperty, value);
    }
    

    }

然后在 MyLogo.xaml 中,我使用以下内容更改了 Fill 属性:

 Fill="{Binding BackgroundFill, RelativeSource={RelativeSource AncestorType=UserControl}}"

最后在我看来触发器所在的位置,我将设置器更改为:

<Setter Property="misc:MyLogo.BackgroundFill" Value="#ffffff" />

就是这样..希望有人会发现这很有用:)

标签: c#wpfxaml

解决方案


推荐阅读