I was getting namespace conflicts when trying solutions to this question. Turns out Brush
and Color
classes exists in two difference namespaces System.Windows.Media
and System.Drawing
and they have similar function names but there are differences too.
If you working with controls like below, it uses Media namespace so make sure you have the correct namespace added wherever you use this objects.
<Button Content="Change Color" Background="AliceBlue"/>
This part in the accepted answer is really right(best) answer but it's good to know about the namespaces.
Brush color = (SolidColorBrush)new BrushConverter().ConvertFromString("Green");
Do note that the following (as suggested in accepted answer) doesn't compile in this namespace.
Color red1 = Color.FromName("Red"); // don't even try, its wrong namespace
Because the Color
class in System.Windows.Media
doesn't have FromName()
member so it's not even applicable.
So so many solutions above but nobody has mentioned namespaces and the answer really depends on that. This question is actually about the Media namespace because that has color names but not mentioning that can confuse new readers who is trying these solutions.
Many people like myself may even not even need the conversion and use the enum as noted in the question itself.
Brush color = Brushes.Red;