简体   繁体   English

如何在xaml和c#中为我的所有Windows中的所有背景添加更改颜色的按钮

[英]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.

相关问题 如何设置xaml C#中所有windows的背景色? - How to set the background color of all windows in xaml C#? C#WPF Xaml:将视图中的所有文本全局设置为一种颜色,将所有背景设置为另一种颜色 - C# WPF Xaml: Globally set all text in a view to one color, and all backgrounds to another 在 C# 中,如何使列表框中的所有项目不更改为我最后设置的颜色? - In C#, how can I keep all my items in a listbox from changing to the color I set last? 如何向所有Windows资源管理器实例添加按钮? - How can i add a button to all windows explorer instances? 为什么当我单击或悬停按钮时按钮会更改颜色? XAML Windows 8 - Why button changes color when I click or hover on it? Xaml windows 8 Windows中的C#RawInputDevice:如何为除我以外的所有应用程序禁用消息? - C# RawInputDevice in Windows: How I can disable messages for all applications except my? 如何添加和获取所有数据库表值(数字)并显示总金额? C# - How can I add and get all my database table values (numbers) and displaying the total amount? C# 如何在我的XAML代码中将C#中的标签添加到网格中? - How can I add a label in C# to a grid in my XAML code? 如何使用XAML在C#应用程序中添加动态组合框 - How can I add Dynamic combobox in my c# application using xaml 如何将应用程序资源添加到应用程序的 C# 后端而不是 XAML 文件中? - How can I add application resources to my C# backend for the application instead of in the XAML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM