在 C# 中将字符串转换为画笔/画笔颜色名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/372693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to Brushes/Brush color name in C#
提问by Clinton Pierce
I have a configuration file where a developer can specify a text color by passing in a string:
我有一个配置文件,开发人员可以在其中通过传入字符串来指定文本颜色:
<text value="Hello, World" color="Red"/>
Rather than have a gigantic switch statement look for all of the possible colors, it'd be nice to just use the properties in the class System.Drawing.Brushes instead so internally I can say something like:
与其用一个巨大的 switch 语句查找所有可能的颜色,不如只使用 System.Drawing.Brushes 类中的属性,这样在内部我可以这样说:
Brush color = Brushes.Black; // Default
// later on...
this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));
Except that the values in Brush/Brushes aren't enums. So Enum.Parse gives me no joy. Suggestions?
除了 Brush/Brushes 中的值不是枚举。所以 Enum.Parse 没有给我带来快乐。建议?
采纳答案by Lucas
Recap of all previous answers, different ways to convert a string to a Color or Brush:
回顾所有以前的答案,将字符串转换为颜色或画笔的不同方法:
// best, using Color's static method
Color red1 = Color.FromName("Red");
// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");
// using Reflection on Color or Brush
Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);
// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
回答by leppie
Try using a TypeConverter
. Example:
尝试使用TypeConverter
. 例子:
var tc = TypeDescriptor.GetConverter(typeof(Brush));
Another alternative is to use reflection, and go over the properties in SystemBrushes
.
另一种选择是使用反射,并检查SystemBrushes
.
回答by Clinton Pierce
D'oh. After a while of looking I found:
哦。经过一段时间的查找,我发现:
Color.FromName(a.Value)
After hitting "post". From there it's a short step to:
点击“发布”后。从那里它是一个简短的步骤:
color = new SolidBrush(Color.FromName(a.Value));
I'll leave this question here for others....
我会把这个问题留在这里给其他人......
回答by Jon B
You could use reflection for this:
您可以为此使用反射:
Type t = typeof(Brushes);
Brush b = (Brush)t.GetProperty("Red").GetValue(null, null);
Of course, you'll want some error handling/range checking if the string is wrong.
当然,如果字符串错误,您将需要一些错误处理/范围检查。
回答by BFree
If you want, you can extend this even more and allow them to specify values for the R, G and B values. Then you just call Color.FromArgb(int r, int g, int b);
如果需要,您可以进一步扩展它并允许他们为 R、G 和 B 值指定值。然后你只需调用 Color.FromArgb(int r, int g, int b);
回答by Brian Rudolph
I agree that using TypeConverters are the best method:
我同意使用 TypeConverters 是最好的方法:
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
return new Brush(c);
回答by Junior Mayhé
String to brush:
要刷的字符串:
myTextBlock.Foreground = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush;
That's my case here!
这就是我的情况!
回答by Lucas
A brush can be declared like this
可以像这样声明画笔
Brush myBrush = new SolidBrush(Color.FromName("Red"));
回答by Const Mi
You can use System.Drawing.KnownColorenum. It specifies all known system colors.
您可以使用System.Drawing.KnownColor枚举。它指定所有已知的系统颜色。