【问题标题】:WPF change Fill Color BrushWPF 更改填充颜色画笔
【发布时间】:2013-01-31 13:09:54
【问题描述】:

在资源字典中,我存储了一个带有 Canvas 的 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>

所以我想使用我的控件容器(称为 IconAsVisualBrush )的 BackgroundColorBrush 更改第二个 Path 的颜色。 这是

<Grid x:Name="GridIconBrush" Width="40" Height="40">
        <Grid.Background>
            <VisualBrush x:Name="CtrlVisualBrush"  Stretch="Uniform" />
        </Grid.Background>
 </Grid>

VisualBrush在cs中设置:

private static void OnIconBrushResourceChanged(DependencyObject source
            , DependencyPropertyChangedEventArgs e)
        {
IconAsVisualBrush control = source as IconAsVisualBrush;
control.CtrlVisualBrush.Visual = (Viewbox)Application.Current.FindResource(e.NewValue);
        }

在我的 UserControl 中,我可以使用以下 xaml 绘制 ViewBox:

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

画布正确绘制但颜色不正确。我收到: 无法通过引用“RelativeSource FindAncestor,AncestorType='IconAsVisualBrush',AncestorLevel='1'”找到绑定源。 BindingExpression:Path=BackgroundColorBrush;数据项=空;目标元素是'路径'(名称='');目标属性是“填充”(输入“画笔”)

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

【问题讨论】:

    标签: wpf binding relative-path


    【解决方案1】:

    您的问题是您的样式中的 Setter 无法找到 IconAsVisualBrush,大概是因为它不是路径的视觉树的一部分。您是否考虑过使用触发器?在不了解您的应用程序架构以及调用 OnIconBrushResourceChanged 的​​内容的情况下,很难提出解决方案 - 但是,由于我们正在讨论 WPF,因此我会做出有根据的猜测,即您正在使用 MVVM。如果是这样,您可以像这样使用 DataTriggers:

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

    编辑:澄清一下,我在这里建议的样式会给你一个默认的白色填充,但是如果你将“MyColourChangedProperty”(或你绑定的任何东西)设置为 true,它会变成红色。

    【讨论】:

    • 我试过数据触发器,但它不起作用,总是默认颜色白色。 OnIconBrushResourceChanged 来自dependecy property public static readonly DependencyProperty IconBrushResourceProperty = DependencyProperty.Register("IconBrushResource", typeof(string), typeof(IconAsVisualBrush) , new PropertyMetadata(string.Empty, OnIconBrushResourceChanged)); 新画笔是在 xaml 代码中设置的。在运行时调用 OnIconBrushResourceChanged 并设置 VisualBrush。可能这里错过了“重新绑定”或“刷新绑定”。
    • 为什么要用字符串来表示画笔?你能告诉我你用于数据触发器的代码吗?