我正在尝试使用WP7.1中提供的所有预定义SolidColorBrushes填充List,但是我在通过代码执行此操作时遇到了问题。我已经用一个简短的测试颜色列表手动完成了这个工作,但是有超过一百种不同的预定义颜色,我知道必须有一个正确的方法,虽然我不确定如何实现这一点。我到目前为止的内容如下
MainPage.xaml中
<ScrollViewer>
<!--<toolkit:MultiselectList x:Name="ColorList" ItemsSource="{Binding}" Height="88" HorizontalAlignment="Left" VerticalAlignment="Top" >-->
<toolkit:MultiselectList x:Name="ColorList" HorizontalAlignment="Left" VerticalAlignment="Top" Tap="ColorList_Tap">
<toolkit:MultiselectList.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="12,12,0,0" Grid.ColumnSpan="2">
<Rectangle Fill="{Binding Brush}" Width="50" Height="50"/>
<TextBlock Text="{Binding Name}" Margin="12,10,0,0"/>
</StackPanel>
</DataTemplate>
</toolkit:MultiselectList.ItemTemplate>
</toolkit:MultiselectList>
</ScrollViewer>
MainPage.xaml.cs中
List<ColorItem> solidColorBrushList;
public ColorListPage()
{
InitializeComponent();
solidColorBrushList = new List<ColorItem>()
{
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,27,161,226)), Name = "blue" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,160,80,0)), Name = "brown" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255, 51,153,51)), Name = "green" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,162,193,57)), Name = "lime" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,216,0,115)), Name = "magenta" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,240,150,9)), Name = "mango" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,230,113,184)), Name = "pink" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,162,0,255)), Name = "purple" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,229,20,0)), Name = "red" },
new ColorItem { Brush = new SolidColorBrush(Color.FromArgb(255,0,171,169)), Name = "teal" }
};
this.ColorList.ItemsSource = solidColorBrushList;
this.Loaded += new RoutedEventHandler(ColorListPage_Loaded);
}
void ColorListPage_Loaded(object sender, RoutedEventArgs e)
{
//show checkboxes when page is loaded
this.ColorList.IsSelectionEnabled = true;
}
}
public class ColorItem
{
public SolidColorBrush Brush { get; set; }
public string Name { get; set; }
}
调用所有画笔及其各自名称以填充多选列表的正确方法是什么? 另外作为旁注,由于某些原因,我的多选列表会在页面中显示时切断文本,是否有办法将列表拉伸到屏幕的宽度,这样矩形和文本都可以显示而不会被切断?
答案 0 :(得分:4)
您可以使用反射来提取Color
中的所有Colors
条目,并从中构建画笔。
你必须原谅我,我现在无法访问Visual Studio,所以我在记事本中将这个问题从头脑中解脱出来。
List<ColorItem> solidColorBrushList = new List<ColorItem>();
PropertyInfo[] colorProperties = typeof(Colors).GetProperties(BindingFlags.Public | BindingFlags.Static)
foreach (PropertyInfo colorProperty in colorProperties)
{
//could probably omit this check; I think all static properties of Colors are of type Color
if (colorProperty.PropertyType == typeof(Color))
{
Color color = (Color)colorProperty.GetValue(null, null);
string colorName = colorPropertyName;
SolidColorBrush brush = new SolidColorBrush(color);
ColorItem item = new ColorItem() { Brush = brush, Name = colorName };
solidColorBrushList.Add(item);
}
}
所以是的,我可能有一个错误的函数参数或者你需要调整BindingFlags
,但这应该给你一个很好的起点。当我明天得到时间(并访问Visual Studio)时,我会仔细检查并在必要时修复它。
编辑:我想把我在工作中粘贴完整的.NET 4.0颜色列表的类粘贴到我们自己的内部Color
结构中,以防你没有使用它。也许它会节省你的时间:
public static class Colors
{
private static readonly Color m_Transparent = Color.FromArgb(0, 255, 255, 255);
private static readonly Color m_AliceBlue = Color.FromRgb(240, 248, 255);
private static readonly Color m_AntiqueWhite = Color.FromRgb(250, 235, 215);
private static readonly Color m_Aqua = Color.FromRgb(0, 255, 255);
private static readonly Color m_Aquamarine = Color.FromRgb(127, 255, 212);
private static readonly Color m_Azure = Color.FromRgb(240, 255, 255);
private static readonly Color m_Beige = Color.FromRgb(245, 245, 220);
private static readonly Color m_Bisque = Color.FromRgb(255, 228, 196);
private static readonly Color m_Black = Color.FromRgb(0, 0, 0);
private static readonly Color m_BlanchedAlmond = Color.FromRgb(255, 235, 205);
private static readonly Color m_Blue = Color.FromRgb(0, 0, 255);
private static readonly Color m_BlueViolet = Color.FromRgb(138, 43, 226);
private static readonly Color m_Brown = Color.FromRgb(165, 42, 42);
private static readonly Color m_BurlyWood = Color.FromRgb(222, 184, 135);
private static readonly Color m_CadetBlue = Color.FromRgb(95, 158, 160);
private static readonly Color m_Chartreuse = Color.FromRgb(127, 255, 0);
private static readonly Color m_Chocolate = Color.FromRgb(210, 105, 30);
private static readonly Color m_Coral = Color.FromRgb(255, 127, 80);
private static readonly Color m_CornflowerBlue = Color.FromRgb(100, 149, 237);
private static readonly Color m_Cornsilk = Color.FromRgb(255, 248, 220);
private static readonly Color m_Crimson = Color.FromRgb(220, 20, 60);
private static readonly Color m_Cyan = Color.FromRgb(0, 255, 255);
private static readonly Color m_DarkBlue = Color.FromRgb(0, 0, 139);
private static readonly Color m_DarkCyan = Color.FromRgb(0, 139, 139);
private static readonly Color m_DarkGoldenrod = Color.FromRgb(184, 134, 11);
private static readonly Color m_DarkGray = Color.FromRgb(169, 169, 169);
private static readonly Color m_DarkGreen = Color.FromRgb(0, 100, 0);
private static readonly Color m_DarkKhaki = Color.FromRgb(189, 183, 107);
private static readonly Color m_DarkMagenta = Color.FromRgb(139, 0, 139);
private static readonly Color m_DarkOliveGreen = Color.FromRgb(85, 107, 47);
private static readonly Color m_DarkOrange = Color.FromRgb(255, 140, 0);
private static readonly Color m_DarkOrchid = Color.FromRgb(153, 50, 204);
private static readonly Color m_DarkRed = Color.FromRgb(139, 0, 0);
private static readonly Color m_DarkSalmon = Color.FromRgb(233, 150, 122);
private static readonly Color m_DarkSeaGreen = Color.FromRgb(143, 188, 139);
private static readonly Color m_DarkSlateBlue = Color.FromRgb(72, 61, 139);
private static readonly Color m_DarkSlateGray = Color.FromRgb(47, 79, 79);
private static readonly Color m_DarkTurquoise = Color.FromRgb(0, 206, 209);
private static readonly Color m_DarkViolet = Color.FromRgb(148, 0, 211);
private static readonly Color m_DeepPink = Color.FromRgb(255, 20, 147);
private static readonly Color m_DeepSkyBlue = Color.FromRgb(0, 191, 255);
private static readonly Color m_DimGray = Color.FromRgb(105, 105, 105);
private static readonly Color m_DodgerBlue = Color.FromRgb(30, 144, 255);
private static readonly Color m_Firebrick = Color.FromRgb(178, 34, 34);
private static readonly Color m_FloralWhite = Color.FromRgb(255, 250, 240);
private static readonly Color m_ForestGreen = Color.FromRgb(34, 139, 34);
private static readonly Color m_Fuchsia = Color.FromRgb(255, 0, 255);
private static readonly Color m_Gainsboro = Color.FromRgb(220, 220, 220);
private static readonly Color m_GhostWhite = Color.FromRgb(248, 248, 255);
private static readonly Color m_Gold = Color.FromRgb(255, 215, 0);
private static readonly Color m_Goldenrod = Color.FromRgb(218, 165, 32);
private static readonly Color m_Gray = Color.FromRgb(128, 128, 128);
private static readonly Color m_Green = Color.FromRgb(0, 128, 0);
private static readonly Color m_GreenYellow = Color.FromRgb(173, 255, 47);
private static readonly Color m_Honeydew = Color.FromRgb(240, 255, 240);
private static readonly Color m_HotPink = Color.FromRgb(255, 105, 180);
private static readonly Color m_IndianRed = Color.FromRgb(205, 92, 92);
private static readonly Color m_Indigo = Color.FromRgb(75, 0, 130);
private static readonly Color m_Ivory = Color.FromRgb(255, 255, 240);
private static readonly Color m_Khaki = Color.FromRgb(240, 230, 140);
private static readonly Color m_Lavender = Color.FromRgb(230, 230, 250);
private static readonly Color m_LavenderBlush = Color.FromRgb(255, 240, 245);
private static readonly Color m_LawnGreen = Color.FromRgb(124, 252, 0);
private static readonly Color m_LemonChiffon = Color.FromRgb(255, 250, 205);
private static readonly Color m_LightBlue = Color.FromRgb(173, 216, 230);
private static readonly Color m_LightCoral = Color.FromRgb(240, 128, 128);
private static readonly Color m_LightCyan = Color.FromRgb(224, 255, 255);
private static readonly Color m_LightGoldenrodYellow = Color.FromRgb(250, 250, 210);
private static readonly Color m_LightGreen = Color.FromRgb(144, 238, 144);
private static readonly Color m_LightGray = Color.FromRgb(211, 211, 211);
private static readonly Color m_LightPink = Color.FromRgb(255, 182, 193);
private static readonly Color m_LightSalmon = Color.FromRgb(255, 160, 122);
private static readonly Color m_LightSeaGreen = Color.FromRgb(32, 178, 170);
private static readonly Color m_LightSkyBlue = Color.FromRgb(135, 206, 250);
private static readonly Color m_LightSlateGray = Color.FromRgb(119, 136, 153);
private static readonly Color m_LightSteelBlue = Color.FromRgb(176, 196, 222);
private static readonly Color m_LightYellow = Color.FromRgb(255, 255, 224);
private static readonly Color m_Lime = Color.FromRgb(0, 255, 0);
private static readonly Color m_LimeGreen = Color.FromRgb(50, 205, 50);
private static readonly Color m_Linen = Color.FromRgb(250, 240, 230);
private static readonly Color m_Magenta = Color.FromRgb(255, 0, 255);
private static readonly Color m_Maroon = Color.FromRgb(128, 0, 0);
private static readonly Color m_MediumAquamarine = Color.FromRgb(102, 205, 170);
private static readonly Color m_MediumBlue = Color.FromRgb(0, 0, 205);
private static readonly Color m_MediumOrchid = Color.FromRgb(186, 85, 211);
private static readonly Color m_MediumPurple = Color.FromRgb(147, 112, 219);
private static readonly Color m_MediumSeaGreen = Color.FromRgb(60, 179, 113);
private static readonly Color m_MediumSlateBlue = Color.FromRgb(123, 104, 238);
private static readonly Color m_MediumSpringGreen = Color.FromRgb(0, 250, 154);
private static readonly Color m_MediumTurquoise = Color.FromRgb(72, 209, 204);
private static readonly Color m_MediumVioletRed = Color.FromRgb(199, 21, 133);
private static readonly Color m_MidnightBlue = Color.FromRgb(25, 25, 112);
private static readonly Color m_MintCream = Color.FromRgb(245, 255, 250);
private static readonly Color m_MistyRose = Color.FromRgb(255, 228, 225);
private static readonly Color m_Moccasin = Color.FromRgb(255, 228, 181);
private static readonly Color m_NavajoWhite = Color.FromRgb(255, 222, 173);
private static readonly Color m_Navy = Color.FromRgb(0, 0, 128);
private static readonly Color m_OldLace = Color.FromRgb(253, 245, 230);
private static readonly Color m_Olive = Color.FromRgb(128, 128, 0);
private static readonly Color m_OliveDrab = Color.FromRgb(107, 142, 35);
private static readonly Color m_Orange = Color.FromRgb(255, 165, 0);
private static readonly Color m_OrangeRed = Color.FromRgb(255, 69, 0);
private static readonly Color m_Orchid = Color.FromRgb(218, 112, 214);
private static readonly Color m_PaleGoldenrod = Color.FromRgb(238, 232, 170);
private static readonly Color m_PaleGreen = Color.FromRgb(152, 251, 152);
private static readonly Color m_PaleTurquoise = Color.FromRgb(175, 238, 238);
private static readonly Color m_PaleVioletRed = Color.FromRgb(219, 112, 147);
private static readonly Color m_PapayaWhip = Color.FromRgb(255, 239, 213);
private static readonly Color m_PeachPuff = Color.FromRgb(255, 218, 185);
private static readonly Color m_Peru = Color.FromRgb(205, 133, 63);
private static readonly Color m_Pink = Color.FromRgb(255, 192, 203);
private static readonly Color m_Plum = Color.FromRgb(221, 160, 221);
private static readonly Color m_PowderBlue = Color.FromRgb(176, 224, 230);
private static readonly Color m_Purple = Color.FromRgb(128, 0, 128);
private static readonly Color m_Red = Color.FromRgb(255, 0, 0);
private static readonly Color m_RosyBrown = Color.FromRgb(188, 143, 143);
private static readonly Color m_RoyalBlue = Color.FromRgb(65, 105, 225);
private static readonly Color m_SaddleBrown = Color.FromRgb(139, 69, 19);
private static readonly Color m_Salmon = Color.FromRgb(250, 128, 114);
private static readonly Color m_SandyBrown = Color.FromRgb(244, 164, 96);
private static readonly Color m_SeaGreen = Color.FromRgb(46, 139, 87);
private static readonly Color m_SeaShell = Color.FromRgb(255, 245, 238);
private static readonly Color m_Sienna = Color.FromRgb(160, 82, 45);
private static readonly Color m_Silver = Color.FromRgb(192, 192, 192);
private static readonly Color m_SkyBlue = Color.FromRgb(135, 206, 235);
private static readonly Color m_SlateBlue = Color.FromRgb(106, 90, 205);
private static readonly Color m_SlateGray = Color.FromRgb(112, 128, 144);
private static readonly Color m_Snow = Color.FromRgb(255, 250, 250);
private static readonly Color m_SpringGreen = Color.FromRgb(0, 255, 127);
private static readonly Color m_SteelBlue = Color.FromRgb(70, 130, 180);
private static readonly Color m_Tan = Color.FromRgb(210, 180, 140);
private static readonly Color m_Teal = Color.FromRgb(0, 128, 128);
private static readonly Color m_Thistle = Color.FromRgb(216, 191, 216);
private static readonly Color m_Tomato = Color.FromRgb(255, 99, 71);
private static readonly Color m_Turquoise = Color.FromRgb(64, 224, 208);
private static readonly Color m_Violet = Color.FromRgb(238, 130, 238);
private static readonly Color m_Wheat = Color.FromRgb(245, 222, 179);
private static readonly Color m_White = Color.FromRgb(255, 255, 255);
private static readonly Color m_WhiteSmoke = Color.FromRgb(245, 245, 245);
private static readonly Color m_Yellow = Color.FromRgb(255, 255, 0);
private static readonly Color m_YellowGreen = Color.FromRgb(154, 205, 50);
public static Color Transparent { get { return m_Transparent; } }
public static Color AliceBlue { get { return m_AliceBlue; } }
public static Color AntiqueWhite { get { return m_AntiqueWhite; } }
public static Color Aqua { get { return m_Aqua; } }
public static Color Aquamarine { get { return m_Aquamarine; } }
public static Color Azure { get { return m_Azure; } }
public static Color Beige { get { return m_Beige; } }
public static Color Bisque { get { return m_Bisque; } }
public static Color Black { get { return m_Black; } }
public static Color BlanchedAlmond { get { return m_BlanchedAlmond; } }
public static Color Blue { get { return m_Blue; } }
public static Color BlueViolet { get { return m_BlueViolet; } }
public static Color Brown { get { return m_Brown; } }
public static Color BurlyWood { get { return m_BurlyWood; } }
public static Color CadetBlue { get { return m_CadetBlue; } }
public static Color Chartreuse { get { return m_Chartreuse; } }
public static Color Chocolate { get { return m_Chocolate; } }
public static Color Coral { get { return m_Coral; } }
public static Color CornflowerBlue { get { return m_CornflowerBlue; } }
public static Color Cornsilk { get { return m_Cornsilk; } }
public static Color Crimson { get { return m_Crimson; } }
public static Color Cyan { get { return m_Cyan; } }
public static Color DarkBlue { get { return m_DarkBlue; } }
public static Color DarkCyan { get { return m_DarkCyan; } }
public static Color DarkGoldenrod { get { return m_DarkGoldenrod; } }
public static Color DarkGray { get { return m_DarkGray; } }
public static Color DarkGreen { get { return m_DarkGreen; } }
public static Color DarkKhaki { get { return m_DarkKhaki; } }
public static Color DarkMagenta { get { return m_DarkMagenta; } }
public static Color DarkOliveGreen { get { return m_DarkOliveGreen; } }
public static Color DarkOrange { get { return m_DarkOrange; } }
public static Color DarkOrchid { get { return m_DarkOrchid; } }
public static Color DarkRed { get { return m_DarkRed; } }
public static Color DarkSalmon { get { return m_DarkSalmon; } }
public static Color DarkSeaGreen { get { return m_DarkSeaGreen; } }
public static Color DarkSlateBlue { get { return m_DarkSlateBlue; } }
public static Color DarkSlateGray { get { return m_DarkSlateGray; } }
public static Color DarkTurquoise { get { return m_DarkTurquoise; } }
public static Color DarkViolet { get { return m_DarkViolet; } }
public static Color DeepPink { get { return m_DeepPink; } }
public static Color DeepSkyBlue { get { return m_DeepSkyBlue; } }
public static Color DimGray { get { return m_DimGray; } }
public static Color DodgerBlue { get { return m_DodgerBlue; } }
public static Color Firebrick { get { return m_Firebrick; } }
public static Color FloralWhite { get { return m_FloralWhite; } }
public static Color ForestGreen { get { return m_ForestGreen; } }
public static Color Fuchsia { get { return m_Fuchsia; } }
public static Color Gainsboro { get { return m_Gainsboro; } }
public static Color GhostWhite { get { return m_GhostWhite; } }
public static Color Gold { get { return m_Gold; } }
public static Color Goldenrod { get { return m_Goldenrod; } }
public static Color Gray { get { return m_Gray; } }
public static Color Green { get { return m_Green; } }
public static Color GreenYellow { get { return m_GreenYellow; } }
public static Color Honeydew { get { return m_Honeydew; } }
public static Color HotPink { get { return m_HotPink; } }
public static Color IndianRed { get { return m_IndianRed; } }
public static Color Indigo { get { return m_Indigo; } }
public static Color Ivory { get { return m_Ivory; } }
public static Color Khaki { get { return m_Khaki; } }
public static Color Lavender { get { return m_Lavender; } }
public static Color LavenderBlush { get { return m_LavenderBlush; } }
public static Color LawnGreen { get { return m_LawnGreen; } }
public static Color LemonChiffon { get { return m_LemonChiffon; } }
public static Color LightBlue { get { return m_LightBlue; } }
public static Color LightCoral { get { return m_LightCoral; } }
public static Color LightCyan { get { return m_LightCyan; } }
public static Color LightGoldenrodYellow { get { return m_LightGoldenrodYellow; } }
public static Color LightGreen { get { return m_LightGreen; } }
public static Color LightGray { get { return m_LightGray; } }
public static Color LightPink { get { return m_LightPink; } }
public static Color LightSalmon { get { return m_LightSalmon; } }
public static Color LightSeaGreen { get { return m_LightSeaGreen; } }
public static Color LightSkyBlue { get { return m_LightSkyBlue; } }
public static Color LightSlateGray { get { return m_LightSlateGray; } }
public static Color LightSteelBlue { get { return m_LightSteelBlue; } }
public static Color LightYellow { get { return m_LightYellow; } }
public static Color Lime { get { return m_Lime; } }
public static Color LimeGreen { get { return m_LimeGreen; } }
public static Color Linen { get { return m_Linen; } }
public static Color Magenta { get { return m_Magenta; } }
public static Color Maroon { get { return m_Maroon; } }
public static Color MediumAquamarine { get { return m_MediumAquamarine; } }
public static Color MediumBlue { get { return m_MediumBlue; } }
public static Color MediumOrchid { get { return m_MediumOrchid; } }
public static Color MediumPurple { get { return m_MediumPurple; } }
public static Color MediumSeaGreen { get { return m_MediumSeaGreen; } }
public static Color MediumSlateBlue { get { return m_MediumSlateBlue; } }
public static Color MediumSpringGreen { get { return m_MediumSpringGreen; } }
public static Color MediumTurquoise { get { return m_MediumTurquoise; } }
public static Color MediumVioletRed { get { return m_MediumVioletRed; } }
public static Color MidnightBlue { get { return m_MidnightBlue; } }
public static Color MintCream { get { return m_MintCream; } }
public static Color MistyRose { get { return m_MistyRose; } }
public static Color Moccasin { get { return m_Moccasin; } }
public static Color NavajoWhite { get { return m_NavajoWhite; } }
public static Color Navy { get { return m_Navy; } }
public static Color OldLace { get { return m_OldLace; } }
public static Color Olive { get { return m_Olive; } }
public static Color OliveDrab { get { return m_OliveDrab; } }
public static Color Orange { get { return m_Orange; } }
public static Color OrangeRed { get { return m_OrangeRed; } }
public static Color Orchid { get { return m_Orchid; } }
public static Color PaleGoldenrod { get { return m_PaleGoldenrod; } }
public static Color PaleGreen { get { return m_PaleGreen; } }
public static Color PaleTurquoise { get { return m_PaleTurquoise; } }
public static Color PaleVioletRed { get { return m_PaleVioletRed; } }
public static Color PapayaWhip { get { return m_PapayaWhip; } }
public static Color PeachPuff { get { return m_PeachPuff; } }
public static Color Peru { get { return m_Peru; } }
public static Color Pink { get { return m_Pink; } }
public static Color Plum { get { return m_Plum; } }
public static Color PowderBlue { get { return m_PowderBlue; } }
public static Color Purple { get { return m_Purple; } }
public static Color Red { get { return m_Red; } }
public static Color RosyBrown { get { return m_RosyBrown; } }
public static Color RoyalBlue { get { return m_RoyalBlue; } }
public static Color SaddleBrown { get { return m_SaddleBrown; } }
public static Color Salmon { get { return m_Salmon; } }
public static Color SandyBrown { get { return m_SandyBrown; } }
public static Color SeaGreen { get { return m_SeaGreen; } }
public static Color SeaShell { get { return m_SeaShell; } }
public static Color Sienna { get { return m_Sienna; } }
public static Color Silver { get { return m_Silver; } }
public static Color SkyBlue { get { return m_SkyBlue; } }
public static Color SlateBlue { get { return m_SlateBlue; } }
public static Color SlateGray { get { return m_SlateGray; } }
public static Color Snow { get { return m_Snow; } }
public static Color SpringGreen { get { return m_SpringGreen; } }
public static Color SteelBlue { get { return m_SteelBlue; } }
public static Color Tan { get { return m_Tan; } }
public static Color Teal { get { return m_Teal; } }
public static Color Thistle { get { return m_Thistle; } }
public static Color Tomato { get { return m_Tomato; } }
public static Color Turquoise { get { return m_Turquoise; } }
public static Color Violet { get { return m_Violet; } }
public static Color Wheat { get { return m_Wheat; } }
public static Color White { get { return m_White; } }
public static Color WhiteSmoke { get { return m_WhiteSmoke; } }
public static Color Yellow { get { return m_Yellow; } }
public static Color YellowGreen { get { return m_YellowGreen; } }
}
答案 1 :(得分:2)
我的答案与克里斯的答案差不多,但我认为值得张贴,因为我得到了一个陈述(并且删除它似乎很可惜):
var colors = typeof(Colors)
.GetProperties(BindingFlags.Static | BindingFlags.Public)
.ToDictionary(p => p.Name,
p => new SolidColorBrush((Color)p.GetValue(null, null))
);
编辑:包括克里斯的建议:
var colors = typeof(Colors)
.GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(p => new ColorItem()
{
Name = p.Name,
Brush = new SolidColorBrush((Color)p.GetValue(null, null))
}
).ToList();
答案 2 :(得分:0)
为了补充企鹅先生的回答,这将 ColorCode 作为字符串值列出,可以像颜色名称本身一样转换 (ColorConverter.ConvertFromString(..)
)。这是一个 Linqpad 输出:
typeof(Colors)
.GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(p => new
{
Name = p.Name,
Brush = new SolidColorBrush((Color)p.GetValue(null, null)),
})
.Select(p => new { p.Name, ColorCode = p.Brush.ToString(), p.Brush})
.Dump();