Reputation: 817
I would like to change the background color of a button when I click on it. To make the button go from Red to Green to Red, etc...
However, when I click the background color doesn't change.
Here is what I have tried :
button4.Background.SetValue(BackgroundProperty,new SolidColorBrush(Colors.Red)); -> catastrophic error
button4.SetValue(BackgroundProperty,new SolidColorBrush(Colors.Red)); -> nothing
button4.Background = new SolidColorBrush(Colors.Red); -> nothing
3rd solution seems the most revelant but doesn't work.
Upvotes: 4
Views: 8794
Reputation: 41
I met the same problem with UserControl in Silverlight 5.
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Background = new SolidColorBrush(Colors.Black); //nothing
}
But if I name the main Grid grid and write down these code:
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
grid.Background = new SolidColorBrush(Colors.Black); //ok
}
It works, I do not know why. When I need to use some complex opacity effects maybe I need some extra Rectangles and set their Fill. It's a little inconvenient.
Upvotes: 4