WPF更改填充颜色笔刷

WPF更改填充颜色笔刷,wpf,binding,relative-path,Wpf,Binding,Relative Path,在资源字典中,我存储了一个带有画布的ViewBox <Style x:Key="MyPathStyle" TargetType="Path"> <Setter Property="Fill" Value="{Binding BackgroundColorBrush, RelativeSource={RelativeSource AncestorType=iconcontrols:IconAsVisualBrush}}"/> </Style&

在资源字典中,我存储了一个带有画布的ViewBox

<Style x:Key="MyPathStyle" TargetType="Path">
    <Setter Property="Fill" Value="{Binding BackgroundColorBrush, 
        RelativeSource={RelativeSource AncestorType=iconcontrols:IconAsVisualBrush}}"/>
</Style>
<Viewbox x:Key="Icon2">
        <Canvas Width="40.000" Height="40.000">
            <Canvas>
                <Path Fill="#ff99baf4" Data="F1 M 14.377,23.798" />
                <Path Style="{StaticResource MyPathStyle}"  Data="..." />
             </Canvas>
         </Canvas>
</Viewbox>
在我的UserControl中,我可以使用xaml绘制ViewBox:

<iconcontrols:IconAsVisualBrush BackgroundColorBrush="White"
                                IconBrushResource="Icon2"/>
<iconcontrols:IconAsVisualBrush BackgroundColorBrush="Red"
                                IconBrushResource="Icon2"/>

画布绘制正确,但颜色不正确。我收到: 找不到引用为“RelativeSource FindAncestor,AncestorType='IconAsVisualBrush',AncestorLevel='1'的绑定源。BindingExpression:Path=BackgroundColorBrush;DataItem=null;目标元素为“路径”(名称=“”);目标属性为“填充”(类型为“笔刷”)


有没有办法只更改所有者控件内的路径填充颜色集(而不是使所有IconAsVisualBrush具有相同颜色的动态资源),以便我可以使用不同的填充颜色绘制相同的形状?

您的问题是,样式中的设置程序无法找到IconAsVisualBrush,大概是因为它不是路径的可视树的一部分。你考虑过使用触发器吗?在不了解您的应用程序体系结构和调用OnIconBrushResourceChanged的内容的情况下,很难提出一个解决方案-但是,既然我们讨论的是WPF,我会有根据地猜测您正在使用MVVM。如果是这样,您可以使用如下数据触发器:

<Style x:Key="MyPathStyle" TargetType="Path">
    <Setter Property="Fill" Value="White" />
    <Style.Triggers>
        <DataTrigger Binding="{MyColourChangedProperty}"
                     Value="True">
            <DataTrigger.Setters>
                <Setter Property="Fill" Value="Red"/>
            </DataTrigger.Setters>
        </DataTrigger>
    </Style.Triggers>
</Style>


编辑:为了澄清,我在这里建议的样式将为您提供默认的白色填充,但是如果您将“MyLourChangedProperty”(或您绑定到的任何内容)设置为true,它将变为红色。

我尝试了数据触发器,但它不起作用,始终是默认的白色。
OnIconBrushResourceChanged
来自
Dependency属性公共静态只读DependencyProperty IconBrushResourceProperty=DependencyProperty.Register(“IconBrushResource”、typeof(string)、typeof(IconAsVisualBrush)、new PropertyMetadata(string.Empty、OnIconBrushResourceChanged))在xaml代码中设置新笔刷。在运行时调用OnIconBrushResourceChanged并设置VisualBrush。这里可能缺少“重新绑定”或“刷新绑定”。为什么要使用字符串来表示画笔?你能给我看看你用来触发数据的代码吗?
<iconcontrols:IconAsVisualBrush BackgroundColorBrush="White"
                                IconBrushResource="Icon2"/>
<iconcontrols:IconAsVisualBrush BackgroundColorBrush="Red"
                                IconBrushResource="Icon2"/>
<Style x:Key="MyPathStyle" TargetType="Path">
    <Setter Property="Fill" Value="White" />
    <Style.Triggers>
        <DataTrigger Binding="{MyColourChangedProperty}"
                     Value="True">
            <DataTrigger.Setters>
                <Setter Property="Fill" Value="Red"/>
            </DataTrigger.Setters>
        </DataTrigger>
    </Style.Triggers>
</Style>