如何在通用Windows平台应用程序中单击事件更改按钮的背景颜色?

我正在使用Windows 10中的UWP应用程序,我正在尝试更改单击事件中按钮的背景颜色。 这是我的代码:

private void button1_1_Click(object sender, RoutedEventArgs e) { if (_Sign) { button_1_1.Content = "Cross"; _Sign = false; } else { // button_1_1.Background = new SolidColorBrush(new Windows.UI.Color ) // indows.UI.Colors clr = new Windows.UI.Colors(new SolidColorBrush red); // SolidColorBrush color = new SolidColorBrush(); // color = new SolidColorBrush. // button_1_1.Background = clr; button_1_1.Content = "Tick"; _Sign = true; } } 

使用“ 颜色”属性中的预定义颜色对象:

 button_1_1.Background = new SolidColorBrush(Windows.UI.Colors.White); 

你可以做到这一点

 button1.SetValue(BackgroundProperty,new SolidColorBrush(Windows.UI.Colors.Black)); 

你可以玩! 我现在不在我的电脑上检查它,但这样的东西有效。

或者你可以试试

 button1.Background = new SolidColorBrush(Windows.Ui.Colors.Black); 

您还可以提供不同的颜色

  SolidColorBrush mySolidColorBrush = new SolidColorBrush(); mySolidColorBrush.Color = Color.FromArgb(0, 255, 244, 244); button1.Background = mySolidColorBrush; 

你必须像这样将颜色代码转换为Argb

 return new SolidColorBrush( Color.FromArgb( 255, Convert.ToByte(hexaColor.Substring(1, 2), 16), Convert.ToByte(hexaColor.Substring(3, 2), 16), Convert.ToByte(hexaColor.Substring(5, 2), 16) ) ); 

它非常简单和正确,因为你可以给任何颜色而不是黑色,橙色等默认颜色。