C# 在 WPF 中使用动态资源设置控件背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17502467/
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
Set control Background color using Dynamic Resource in WPF?
提问by iAteABug_And_iLiked_it
This is my XAML
这是我的 XAML
<Grid.Resources>
<SolidColorBrush x:Key="DynamicBG"/>
</Grid.Resources>
<Label name="MyLabel" Content="Hello" Background="{DynamicResource DynamicBG} />
So I have two questions:
所以我有两个问题:
Q1:How do I go about setting the DynamicBGkey value to Redin my code now? (When the window loads, I'd like to set it to red)
Q1:我现在如何在我的代码中将DynamicBG键值设置为红色?(当窗口加载时,我想将其设置为红色)
Q2:Is this how dynamic resources are supposed to be used?
Q2:动态资源应该这样使用吗?
Thank you
谢谢
采纳答案by Anatoliy Nikolaev
To gain access to the Resourceof the code must identify them in the file App.xaml:
要获得Resource对代码的访问,必须在文件中标识它们App.xaml:
<Application.Resources>
<SolidColorBrush x:Key="DynamicBG" />
</Application.Resources>
XAML example
XAML example
<Grid>
<Label Name="MyLabel"
Content="Hello"
Background="{DynamicResource DynamicBG}" />
<Button Content="Change color"
Width="100"
Height="30"
Click="Button_Click" />
</Grid>
The Resourcecan be changed in code line of the form:
的Resource可以在形式的代码线进行更改:
Application.Current.Resources["MyResource"] = MyNewValue;
Example:
例子:
Code behind
Code behind
// using ContentRendered event
private void Window_ContentRendered(object sender, EventArgs e)
{
SolidColorBrush MyBrush = Brushes.Aquamarine;
// Set the value
Application.Current.Resources["DynamicBG"] = MyBrush;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush MyBrush = Brushes.CadetBlue;
// Set the value
Application.Current.Resources["DynamicBG"] = MyBrush;
}
Principle, DynamicResourceswere designed, so they can be changed. Where to change - it is the task of the developer. In the case of Color, it is one of the most common methods. See the MSDN, for more information.
原则,DynamicResources被设计,所以他们可以改变。在哪里更改 - 这是开发人员的任务。在 的情况下Color,它是最常用的方法之一。有关更多信息,请参阅MSDN。
P. S. I recommend using App.xaml, because there have been cases where a StaticResourcehas been used successfully, but not DynamicResource(resources are placed in the Window.Resources). But after moving the resource in App.xaml, everything started to work.
PS 我推荐使用App.xaml,因为曾经有过StaticResource成功使用a 的情况,但没有成功DynamicResource(资源放在 中Window.Resources)。但是在将资源移入 后App.xaml,一切都开始工作了。
回答by kmatyaszek
A1:
You should move "DynamicBG" to window resource and after that you can use Resourcesproperty in Loadedevent handler:
A1:您应该将“DynamicBG”移动到窗口资源,然后您可以Resources在Loaded事件处理程序中使用属性:
XAML:
XAML:
<Window x:Class="MyLabelDynamicResource.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="Window_Loaded">
<Window.Resources>
<SolidColorBrush x:Key="DynamicBG"/>
</Window.Resources>
<Grid>
<Label Name="MyLabel" Content="Hello" Background="{DynamicResource DynamicBG}" />
</Grid>
</Window>
Code-behind:
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Resources["DynamicBG"] = new SolidColorBrush(Colors.Red);
}
}
A2: You should use dynamic resources when you want to change property in runtime.
A2:当您想在运行时更改属性时,您应该使用动态资源。
回答by Anton Tykhyy
A2: no.To do what you are doing, it is better to use data binding. Have a property in your viewmodel indicating whether it's 'loaded', then bind the background to it with a suitable converter, or use a trigger. (If it's actually UI that is loading, add the property to the window.) Dynamic resources are used for theming and with templates, in the rare cases when a StaticResource lookup happens too early.
A2:没有。要做你正在做的事情,最好使用数据绑定。在您的视图模型中有一个属性,指示它是否已“加载”,然后使用合适的转换器将背景绑定到它,或者使用触发器。(如果实际上是 UI 正在加载,请将属性添加到窗口。)动态资源用于主题化和模板,在极少数情况下,StaticResource 查找发生得太早。

