[英]Change all buttons background color of WPF window
I am trying to add the style dynamically to button,below is the code i am using but it is not working for me 我正在尝试将样式动态添加到按钮,下面是我正在使用的代码,但对我不起作用
First Way 第一路
public Login()
{
InitializeComponent();
Style style = new Style
{
TargetType = typeof(Button)
};
style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Red));
Resources.Add(typeof(Button), style);
}
second way one more thing i have tried with below second way also but it gives me error After a 'SetterBase' is in use (sealed), it cannot be modified 第二种方法我在第二种方法下还尝试了另一种方法,但是它给我错误在使用“ SetterBase”(密封)后,无法对其进行修改
Style style = this.FindResource("ButtonStyle1") as Style;
Setter setter = (Setter)style.Setters[0];
setter.Property. = false;
Color orange1 = (Color)ColorConverter.ConvertFromString("#FFF3800C");
setter.Value = new SolidColorBrush(orange1);
In your Windows Resources add 在Windows资源中添加
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</Window.Resources>
This will change background of all buttons in window 这将更改窗口中所有按钮的背景
This article starts describes a solution, although it stops short of a detailed breakdown; 本文从开始描述了一个解决方案,尽管没有详细的分类。 you should find all you need by looking at the author's code here .
您应该在这里查看作者的代码,找到所需的一切。
The main class is the ThemeManager
which provides a method to apply a theme to the application. 主类是
ThemeManager
,它提供一种将主题应用于应用程序的方法。 The author represents a theme using a Theme
class - the important bit is the Uri
property. 作者使用
Theme
类表示一个主题-重要的是Uri
属性。 Essentially the ThemeManager
loads the appropriate resource file from the Uri, and adds it to the application's MergedDictionaries collection (removing any existing theme resource first). 本质上,
ThemeManager
从Uri加载适当的资源文件,并将其添加到应用程序的MergedDictionaries集合中(首先删除任何现有的主题资源)。
So a condensed (and untested) version of the author's code might go something like this:- 因此,作者代码的精简版(未经测试)可能如下所示:-
private ResourceDictionary _currentTheme;
public void ApplyTheme(Uri uri)
{
if (_currentTheme != null)
{
// Unload the current theme.
Application.Current.Resources.MergedDictionaries.Remove(_currentTheme);
}
var resourceDictionary =
Application.LoadComponent(uri) as ResourceDictionary;
if (resourceDictionary != null)
{
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
_currentTheme = resourceDictionary;
}
}
An example of a Uri would be: 一个Uri的例子是:
var uri = new Uri(
"/Wpf.TestHarness;component/Themes/RedTheme.xaml",
UriKind.Relative);
where Wpf.TestHarness
is the name of the compiled assembly (without the .DLL or .EXE extension) containing the XAML resource files, and /Themes/RedTheme.xaml' is the path to one of the XAML theme files embedded in that project (
component` is a required part of this "pack URI" path notation). 其中
Wpf.TestHarness
是包含XAML资源文件的已编译程序集的名称(不带.DLL或.EXE扩展名),而/Themes/RedTheme.xaml' is the path to one of the XAML theme files embedded in that project (
组件”是此“ pack URI”路径符号的必需部分)。 Remember to set the build action of the XAML files in the project to "Page". 请记住,将项目中XAML文件的生成操作设置为“页面”。
Now, create a style called "ButtonStyle1" in each of the theme files, but with different background colours for example. 现在,在每个主题文件中创建一个名为“ ButtonStyle1”的样式,但是具有不同的背景颜色。 In your application I think you need to reference such styles using
DynamicResource
(someone correct me if I'm wrong), eg:- 在您的应用程序中,我认为您需要使用
DynamicResource
引用此类样式(如果我错了,请纠正我),例如:-
<Button Style="{DynamicResource ButtonStyle1}" ... />
Better still, don't name your styles. 更好的是,不要命名您的样式。 Just use the
TargetType
, to apply it to every control of that type:- 只需使用
TargetType
,即可将其应用于该类型的每个控件:
<Style TargetType="{x:Type Button}">...</Style>
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.