How to change the button background color in universal applications for the Windows platform?

I am working on a UWP application on Windows 10. Trying to change the background color of a button on a click event. I searched the internet about this, but could not help. This is my code. if anyone has an idea how to do this. please share.

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; } } 
+5
source share
3 answers

Use the predefined color objects from Color Properties :

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

You can do just that

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

You can play with it! I am not on my computer to test it, but something similar works.

or you can try

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

You can also specify a different color.

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

U just need to convert the color code to Argb like this

 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) ) ); 

Its very easy and correct, because you can give any color not by default, like black, orange, etc.

0
source

Source: https://habr.com/ru/post/1243247/


All Articles