我如何从颜色或画笔类中获得所有标准颜色

时间:2014-02-13 15:26:51

标签: c# colors

如何从颜色或画笔类中获取所有标准颜色?

例如:

Foreach var item in  brushes
     Do something...

2 个答案:

答案 0 :(得分:4)

foreach(var color in typeof(Brushes)
                    .GetProperties(BindingFlags.Static | BindingFlags.Public))
{
    var currentColor = color.GetValue(null) as Brush;
}

如果您只想要颜色名称,那么您可以使用LINQ

 var colorNames = typeof (Brushes)
            .GetProperties(BindingFlags.Static | BindingFlags.Public)
            .Select(x => x.Name);

答案 1 :(得分:4)

var colors = Enum.GetValues(typeof(KnownColor)).Cast<KnownColor>().ToList();

var color = Color.FromKnownColor(colors[0]); //for ex.

或直接

var colors = Enum.GetValues(typeof(KnownColor)).Cast<KnownColor>()
                    .Select(k => Color.FromKnownColor(k))
                    .ToList();