I am building a Windows Store application. I am facing a problem when trying to compare the background with a color.
What My Program Does. There are many buttons on the screen and on a click of any button it changes the background color of it to either red or green. Starting from Red and switching color per click.Now I want that the buttons that already have been clicked, their background
should not change. Thus the background checking if
statement to skip the background
color change code.
This is my code:
private void changecolor(object sender, RoutedEventArgs e)
{
if ((sender as Button).Background != "Red" && (sender as Button).Background != "Green")
{
if (counter == 1)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Green);
(sender as Button).Content = "Green";
counter = 0;
}
else if (counter == 0)
{
(sender as Button).Background = new SolidColorBrush(Windows.UI.Colors.Red);
(sender as Button).Content = "Red";
counter = 1;
}
}
}
On the first if
statement, I want to check if the Background
is not Red
or Green
.
(sender as Button).Background != Windows.UI.Colors.Red
(sender as Button).Background != "Red"
The above code doesn't work.
What Do I write in Place of "Red" to make the comparison work?
I finally have gotten the answer to this.
I caste the
background
as asolidcolorbrush
then used itscolor
property and compared it to theWindows.Ui.Colors.Green
Here is the code.