[英]How can I add a button that changes color for all backgrounds in all my Windows in xaml and c#
我已經更改了作為我的按鈕的窗口(標簽)的背景顏色,但是其他窗口中的其他標簽也沒有更改它。 有人能幫我嗎?
private void Button_Click(object sender, RoutedEventArgs e)
{
label3.Background = new SolidColorBrush(Colors.Green);
}
添加Style作為Window.Resources或Application.Resources如下:
<Window.Resources>
<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="White"/>
</Style>
</Window.Resources>
我添加了按鈕,如下所示:
<Button Name="Button1" Click="Button_Click" Style="{DynamicResource MyButtonStyle}">Button1</Button>
並且,為“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;
}
單擊按鈕時,按鈕的顏色將更改為綠色。
它來自https://mobilechos.blogspot.com/2019/04/how-to-change-button-background-color.html我的博客是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.