如何在运行时更改资源字典颜色

时间:2022-11-21 15:00:01

In XAML:

<ResourceDictionary>
    <Color x:Key="BrushColor1">Red</Color>
    <Color x:Key="BrushColor2">Black</Color>

    <LinearGradientBrush x:Key="GradientBrush1" StartPoint="0,0.5" EndPoint="1,0.5">
        <GradientStop Color="{DynamicResource BrushColor2}" Offset="0" />
        <GradientStop Color="{DynamicResource BrushColor1}" Offset="1" />
    </LinearGradientBrush>
</ResourceDictionary>

In C#:

    public void CreateTestRect()
    {
        Rectangle exampleRectangle = new Rectangle();
        exampleRectangle.Width = 150;
        exampleRectangle.Height = 150;
        exampleRectangle.StrokeThickness = 4;
        exampleRectangle.Margin = new Thickness(350);

        ResourceDictionary resources = this.Resources;
        resources["BrushColor1"] = Colors.Pink;
        resources["BrushColor2"] = Colors.RoyalBlue;
        Brush brush =(Brush)this.FindResource("GradientBrush1");

        exampleRectangle.Fill = brush;
        canvas.Children.Insert(0, exampleRectangle);
    }

How to change those Color elements in the runtime in C#. The LinearGradientBrush should get changed dynamically?

如何在C#中更改运行时中的那些Color元素。 LinearGradientBrush应该动态更改?

I wish to do something like this:

我希望做这样的事情:

(Color)(this.FindResource("BrushColor1")) = Colors.Pink;

but I failed.

但我失败了。

2 个解决方案

#1


5  

You can directly overwrite the value in the resource dictionary.

您可以直接覆盖资源字典中的值。

For example:

ResourceDictionary resources = this.Resources; // If in a Window/UserControl/etc
resources["BrushColor1"] = System.Windows.Media.Colors.Black;

That being said, I would normally recommend not doing this. Instead, I would have two sets of colors, each with their own key, and use some mechanism to switch which color is assigned to your values at runtime instead. This lets you leave the entire logic for this in the xaml itself.

话虽如此,我通常建议不要这样做。相反,我会有两组颜色,每组都有自己的键,并使用一些机制来切换在运行时为您的值分配哪种颜色。这使您可以在xaml本身中保留整个逻辑。

#2


2  

I just solved it. Looks like only LinearGradientBrush does not support DynamicResource for color. Even u overwrite the dynamic color, the LinearGradientBrush itself won't get updated. PS: SolidColorBrush supports runtime color change. Do something below instead:

我刚刚解决了看起来只有LinearGradientBrush不支持DynamicResource颜色。即使你覆盖动态颜色,LinearGradientBrush本身也不会更新。 PS:SolidColorBrush支持运行时颜色更改。请改为:

LinearGradientBrush linearGradientBrush = (LinearGradientBrush)(this.FindResource("GradientBrush1"));
linearGradientBrush.GradientStops[0].Color = color1;
linearGradientBrush.GradientStops[1].Color = color2;

If the LinearGradientBrush is hidden in a nested complex brush defined in Resource Dictionary, you make the LinearGradientBrush emerged, and assign a key to it.

如果LinearGradientBrush隐藏在资源字典中定义的嵌套复杂画笔中,则会使LinearGradientBrush出现,并为其指定一个键。

#1


5  

You can directly overwrite the value in the resource dictionary.

您可以直接覆盖资源字典中的值。

For example:

ResourceDictionary resources = this.Resources; // If in a Window/UserControl/etc
resources["BrushColor1"] = System.Windows.Media.Colors.Black;

That being said, I would normally recommend not doing this. Instead, I would have two sets of colors, each with their own key, and use some mechanism to switch which color is assigned to your values at runtime instead. This lets you leave the entire logic for this in the xaml itself.

话虽如此,我通常建议不要这样做。相反,我会有两组颜色,每组都有自己的键,并使用一些机制来切换在运行时为您的值分配哪种颜色。这使您可以在xaml本身中保留整个逻辑。

#2


2  

I just solved it. Looks like only LinearGradientBrush does not support DynamicResource for color. Even u overwrite the dynamic color, the LinearGradientBrush itself won't get updated. PS: SolidColorBrush supports runtime color change. Do something below instead:

我刚刚解决了看起来只有LinearGradientBrush不支持DynamicResource颜色。即使你覆盖动态颜色,LinearGradientBrush本身也不会更新。 PS:SolidColorBrush支持运行时颜色更改。请改为:

LinearGradientBrush linearGradientBrush = (LinearGradientBrush)(this.FindResource("GradientBrush1"));
linearGradientBrush.GradientStops[0].Color = color1;
linearGradientBrush.GradientStops[1].Color = color2;

If the LinearGradientBrush is hidden in a nested complex brush defined in Resource Dictionary, you make the LinearGradientBrush emerged, and assign a key to it.

如果LinearGradientBrush隐藏在资源字典中定义的嵌套复杂画笔中,则会使LinearGradientBrush出现,并为其指定一个键。