在WPF中在运行时更改样式 [英] Changing the styles at runtime in WPF
问题描述
我试图允许用户自定义WPF应用程序中的元素。我要实现的目标是,如果我有一个列表框指定所有表单元素(文本框,标签等),则用户可以选择一个表单元素,并将样式属性设置为Label,前景应为橙色, TextBox前景应为黑色,依此类推。而且,按照我打算应用的所有样式,所有TextBox看起来都应该相似。
I am trying to allow the user to customize the elements in a WPF application. What I am trying to achieve is, if I have a list box which specifies all the form elements (TextBox, label etc.) user can pick one form element, and set the style property say Label, foreground should be in orange where as for TextBox foreground should be in black and so on. And as per what ever style I am intending to apply all the TextBoxes should look alike.
我无法找到实现此目的的方法。我已经尝试了一个示例,其中可以在运行时上载多个预定义的样式。所以现在,我想找到一种在运行时更改不同元素的属性的方法。
I am not able to go figure out a way for achieving this. I have tried out an example where multiple pre-defined styles can be uploaded at runtime. So now, I would like to find a way of changing the property of different elements at runtime.
UPDATE:
UPDATE:
我尝试根据后面的代码创建新样式。
I tried to create a new style from the code behind.
XAML
<Label Content="SAMPLE" Style="{DynamicResource Style1}" x:Name="label1" />
<Button Content="Button" Click="Button_Click" />
,在后面的代码中,即单击按钮时,我尝试了此操作:
and in code behind i.e. on click of the Button I tried this:
Style style = new Style { TargetType = typeof(Label) };
style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Black));
Application.Current.Resources["Style1"] = style;
但是它没有得到更新。
谢谢。
推荐答案
您必须确保样式位于文件 App.xaml
:
You have to make sure that the styles are in the file App.xaml
:
<Application x:Class="ChangeStyleHelp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="MyStyle" TargetType="{x:Type Label}">
<Setter Property="Background" Value="Green" />
</Style>
</Application.Resources>
</Application>
后面的代码:
private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
Style style = new Style
{
TargetType = typeof(Label)
};
style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));
Application.Current.Resources["MyStyle"] = style;
}
如果 Style
在 Window
( Window.Resources
)的资源中,则需要编写 this
或 Window
的名称:
If the Style
is in the resource of Window
(Window.Resources
), then you need to write this
, or the name of the Window
:
private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
Style style = new Style
{
TargetType = typeof(Label)
};
style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));
this.Resources["MyStyle"] = style;
}
同样,您可以更改 Style
这样:采用现有样式并使用元素。示例:
As well, you can change the Style
this way: take an existing style and use of the element. Example:
<Application x:Class="ChangeStyleHelp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="AnotherWayStyle" TargetType="{x:Type Label}">
<Setter Property="Background" Value="Lavender" />
<Setter Property="Foreground" Value="OrangeRed" />
</Style>
</Application.Resources>
</Application>
后面的代码:
private void AnotherWay_Click(object sender, RoutedEventArgs e)
{
label1.Style = (Style)Application.Current.Resources["AnotherWayStyle"];
}
这篇关于在WPF中在运行时更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!