C# 如何在列表框中加载所有已知颜色?
我想在我的C# 如何在列表框中加载所有已知颜色?,c#,wpf,c#-4.0,C#,Wpf,C# 4.0,我想在我的列表框中加载所有预定义的颜色。我也用同样的字体,但我不知道颜色。我的意思是,我不知道如何列举所有已知的颜色 foreach (FontFamily F in Fonts.SystemFontFamilies) { ListBoxItem l = new ListBoxItem(); l.Content = F.ToString(); l.FontFamily = F; lis
列表框中加载所有预定义的颜色。我也用同样的字体,但我不知道颜色。我的意思是,我不知道如何列举所有已知的颜色
foreach (FontFamily F in Fonts.SystemFontFamilies)
{
ListBoxItem l = new ListBoxItem();
l.Content = F.ToString();
l.FontFamily = F;
listbox1.Items.Add(l);
}
如何对Color
执行相同的操作?Color
是一种结构,因此不在IEnumerable
的范围内,因此您必须执行以下操作:
foreach (Color color in Enum.GetValues(typeof(KnownColor))
{
ListBoxItem l = new ListBoxItem();
l.Content = color.Name;
l.Color = color;
listbox1.Items.Add(l);
}
Color
是一种结构,因此不在IEnumerable
的范围内,因此您必须:
foreach (Color color in Enum.GetValues(typeof(KnownColor))
{
ListBoxItem l = new ListBoxItem();
l.Content = color.Name;
l.Color = color;
listbox1.Items.Add(l);
}
你也可以试试这个
private List finalColorList()
{
字符串[]allColors=Enum.GetNames(typeof(System.Drawing.KnownColor));
字符串[]SystemEnvironmentColor=
新字符串[(
typeof(System.Drawing.SystemColors)).GetProperties().Length];
int指数=0;
foreach(成员信息中的成员)(
typeof(System.Drawing.SystemColors)).GetProperties()
{
SystemEnvironmentColor[index++]=member.Name;
}
List finalColorList=新列表();
foreach(所有颜色中的字符串颜色)
{
if(Array.IndexOf(systemEnvironmentColors,color)<0)
{
最终颜色列表。添加(颜色);
}
}
返回最终颜色列表;
}
您也可以试试这个
private List finalColorList()
{
字符串[]allColors=Enum.GetNames(typeof(System.Drawing.KnownColor));
字符串[]SystemEnvironmentColor=
新字符串[(
typeof(System.Drawing.SystemColors)).GetProperties().Length];
int指数=0;
foreach(成员信息中的成员)(
typeof(System.Drawing.SystemColors)).GetProperties()
{
SystemEnvironmentColor[index++]=member.Name;
}
List finalColorList=新列表();
foreach(所有颜色中的字符串颜色)
{
if(Array.IndexOf(systemEnvironmentColors,color)<0)
{
最终颜色列表。添加(颜色);
}
}
返回最终颜色列表;
}
您可以从
是检索颜色的示例。您可以从
是检索颜色的示例。我在WPF中使用此代码,但它显示了错误:名称空间“System”中不存在类型或名称空间名称“Drawing”(是否缺少程序集引用?)KnownColor
是C中的enum
,它存储所有命名颜色的值@user1050667i正在WPF中使用此代码,但它向我显示错误:命名空间“System”中不存在类型或命名空间名称“Drawing”(是否缺少程序集引用?KnownColor
是C#中的enum
,它存储所有命名颜色的值@用户1050667
private List finalColorList()
{
string[] allColors = Enum.GetNames(typeof(System.Drawing.KnownColor));
string[] systemEnvironmentColors =
new string[(
typeof(System.Drawing.SystemColors)).GetProperties().Length];
int index = 0;
foreach (MemberInfo member in (
typeof(System.Drawing.SystemColors)).GetProperties())
{
systemEnvironmentColors[index ++] = member.Name;
}
List finalColorList = new List();
foreach (string color in allColors)
{
if (Array.IndexOf(systemEnvironmentColors, color) < 0)
{
finalColorList.Add(color);
}
}
return finalColorList;
}
KnownColor[] colors = Enum.GetValues(typeof(KnownColor));
foreach(KnownColor knowColor in colors)
{
Color color = Color.FromKnownColor(knowColor);
ListBoxItem listItem = new ListBoxItem();
listItem.Content = color.ToString();
listItem.Color = color ;
listbox1.Items.Add(listItem);
}