Change background color of button in silverlight 5 in code behind

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.

Answers


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.


Need Your Help

Swing: vertically stacked components without MigLayout

java swing layout-manager

I finally got the behavior I want for vertically stacking components that have a preferred height that changes with time. But I needed to use MigLayout.

Load a JavaScript module, store function result on variable and then console log

javascript node.js steam node-modules

I'm new to JavaScript/Node.js and I'm trying to learn Asynchronous calls and callbacks.