How to change the background color of button in Universal Windows Platform Apps?

By : Ammar
Source: Stackoverflow.com
Question!

I'm working on a UWP app in windows 10 . Trying to change the background color of button on click event . I search on internet about this but could't get any help. This is my code . if anyone has idea how to do it . 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;
        }
    }
By : Ammar


Answers

You can also provide different color

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

U have to just convert a 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 proper because you can give any color not a default color like black ,orange etc.



You can do just that

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

You can play with that! I am not on my pc now to check it but something like that works.

or you can try

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


This video can help you solving your question :)
By: admin