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
MessageBackgroundproperty to aSolidColorBrushto be able to set itsColorproperty:This is not any better than simply setting the property to a new
SolidColorBrushthough so your current approach is perfectly fine.