How I can cast color name to SolidColorBrush
type? I mean the word i.e. "Yellow".
SolidColorBrush scb = ??? ; // "Yellow"
Thank you!
For getting the color, use:
Color col=(Color)ColorConverter.ConvertFromString("Red");
Then create your brush:
Brush brush=new SolidColorBrush(col);
or if you can use the Colors-enum
Brush brush=new SolidColorBrush(Colors.Red);
If you already know the name of the color you can get the brush directly from Brushes
:
SolidColorBrush scb = Brushes.Yellow; //scb seems a bit redundant at this point...
In code you should usually not use converters unless you have a string whose value you do not know.
You cannot cast one to another. They are simply different concepts. A brush is brush and color is, well, a color. Just because a brush "paints" in a specific color, doesn't mean you can interchange one with another.
You can however create a SolidColorBrush with a specific color, for example:
var brush = new SolidColorBrush(Color.Yellow);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With