[英]How can I add a button that changes color for all backgrounds in all my Windows in xaml and c#
I have already changed the background color of the window (a label) that are my buttons, but it doesn't appear the other labels from the other windows to change it too. 我已经更改了作为我的按钮的窗口(标签)的背景颜色,但是其他窗口中的其他标签也没有更改它。 Can someone help me?
有人能帮我吗?
private void Button_Click(object sender, RoutedEventArgs e)
{
label3.Background = new SolidColorBrush(Colors.Green);
}
Add Style as Window.Resources or Application.Resources as below: 添加Style作为Window.Resources或Application.Resources如下:
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="White"/>
</Style>
</Window.Resources>
I added button as an example as below: 我添加了按钮,如下所示:
<Button Name="Button1" Click="Button_Click" Style="{DynamicResource MyButtonStyle}">Button1</Button>
And, Add event handler for "Button_Click" as below: 并且,为“Button_Click”添加事件处理程序,如下所示:
public void Button_Click(object sender, EventArgs e)
{
Style style = new Style { TargetType = typeof(Button) };
style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Green));
Resources["MyButtonStyle"] = style;
}
Button's color will be changed to Green when you click the button. 单击按钮时,按钮的颜色将更改为绿色。
It comes from https://mobilechos.blogspot.com/2019/04/how-to-change-button-background-color.html what is my blog. 它来自https://mobilechos.blogspot.com/2019/04/how-to-change-button-background-color.html我的博客是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.