[英]Cast Color Name to SolidColorBrush
How I can cast color name to SolidColorBrush
type? 我如何将 颜色名称转换为
SolidColorBrush
类型? I mean the word ie "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 或者如果你可以使用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
: 如果您已经知道颜色的名称,可以直接从
Brushes
获取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: 但是,您可以创建具有特定颜色的SolidColorBrush ,例如:
var brush = new SolidColorBrush(Color.Yellow);
// Yellow is green + red
SolidColorBrush yellowBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 255, 0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.