Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast Color Name to SolidColorBrush

Tags:

c#

wpf

How I can cast color name to SolidColorBrush type? I mean the word i.e. "Yellow".

SolidColorBrush scb =  ??? ; // "Yellow" 

Thank you!

like image 844
Friend Avatar asked Feb 14 '12 12:02

Friend


3 Answers

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);
like image 77
HCL Avatar answered Sep 30 '22 04:09

HCL


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.

like image 23
H.B. Avatar answered Sep 30 '22 06:09

H.B.


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);
like image 31
Christian.K Avatar answered Sep 30 '22 06:09

Christian.K