Search code examples
c#uwpvisual-studio-2017background-color

How to change the backcolor of a Button in UWP


There are a few identical questions in SO asking about changing the background color of a button in UWP (Universal Windows Platform):

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

how to change background color of button in UWP Apps in c# ?

However, it seems that Microsoft has been changing things. SolidColorBrush can no longer be found. I have tried using Windows.UI.Xaml.Media.Brush and Windows.UI.Colors. None of those work.

Mouse hovering over button.background, the hint shows that background is expecting type: Windows.UI.Xaml.Media.Brush.

enter image description here

My Question: How do I change the background color of a button using c# codes? If I use the suggested solutions in other identical posts, namespace SolidColorBrush will not be found.


Solution

  • Have you imported the following namespace?

    Windows.UI.Xaml.Media
    

    Because if have not, you won't be access the SolidColorBrush Class directly, and will have to do so by:

    Windows.UI.Xaml.Media.SolidColorBrush mycolor = new SolidColorBrush(Windows.UI.Colors.Blue);
    

    In this example, I have created a SolidColorBrush with the Color of Blue, and I have setit directly, without any kind of conversion, like this:

    myButton.Background = mycolor;
    

    In case you want to create your own color, you can utilize the Windows.UI.Color.FromArgb method, where you can even specify the alpha of your Color.

    Edit:

    Looking back on your answer, I have realized that you were trying to create your Brush, which would set the Background of a button control, with the Brush class. The Brush class is the parent for several derived brushes classes, all with different purposes. One of them is the SolidColorBrush class.