I have a brush that is binded to the background of an element in my wpf app.
When I initialize it in the constructor, I do so accordingly:
MessageBackground = new SolidColorBrush(Colors.Red);
Now there are times in the code where I want to change the background to green. I have been doing it like so:
MessageBackground = new SolidColorBrush(Colors.LightGreen);
but this seems wrong to me. Message background itself is type System.Windows.Media.Brush. It does not appear to have any property like Color or Brush that would permit dynamically changing it.
You will need to cast the MessageBackground
property to a SolidColorBrush
to be able to set its Color
property:
var brush = MessageBackground as SolidColorBrush;
if (brush != null)
brush.Color = Colors.LightGreen;
This is not any better than simply setting the property to a new SolidColorBrush
though so your current approach is perfectly fine.