wpf 如何在运行时更改资源字典颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13403606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Resource Dictionary Color in the runtime
提问by dongx
In XAML:
在 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#:
在 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.
但我失败了。
回答by Reed Copsey
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 本身中。
回答by dongx
I just solved it. Looks like only LinearGradientBrushdoes not support DynamicResourcefor color. Even u overwrite the dynamic color, the LinearGradientBrushitself won't get updated. PS: SolidColorBrushsupports 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 LinearGradientBrushis hidden in a nested complex brush defined in Resource Dictionary, you make the LinearGradientBrushemerged, and assign a key to it.
如果LinearGradientBrush隐藏在资源字典中定义的嵌套复杂画笔中,则使LinearGradientBrush出现,并为其分配一个键。

