Changing button background color in silverlight 5 in code

I want to change the background color of a button when I click on it. So that the button switches from red to green to red, etc.

However, when I click, the background color does not change.

Here is what I 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 

The 3rd solution seems the most optimistic, but does not work.

+4
source share
1 answer

I met the same issue 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 of the grid and write the following code:

 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); grid.Background = new SolidColorBrush(Colors.Black); //ok } 

It works, I don’t know why. When I need to use some complex opacity effects, I may need additional Rectangles and install Fill. This is a bit uncomfortable.

+4
source

Source: https://habr.com/ru/post/1399750/


All Articles