[英]Loading a combo box with all system colors in wpf
I have created combo box that I would like to load with all of the standard colors.我已经创建了我想加载所有标准 colors 的组合框。 I would like to do this in the xaml.cs file rather than the straight XAML.我想在 xaml.cs 文件中执行此操作,而不是直接的 XAML。 I have found many examples to do this in the C# for .NET but not WPF.我在 C# 中找到了许多示例,用于 .NET 但不是 WPF。
I found the following code that runs in .NET and it seems that prop.PropertyType.FullName
never equals "System.Drawing.Color")
I debugged through it and the only value that System.Reflection.PropertyInfo eqauls that makes sense is System.Windows.Media.ColorContext.我发现以下代码在 .NET 中运行,似乎prop.PropertyType.FullName
永远不等于"System.Drawing.Color")
我通过它进行了调试,并且 System.Reflection.PropertyInfo 唯一有意义的值是 System.Windows .Media.ColorContext。 But when i tried this it did not return any colors.但是当我尝试这个时,它没有返回任何 colors。
foreach (System.Reflection.PropertyInfo prop in typeof(Color).GetProperties())
{
if (prop.PropertyType.FullName == "System.Drawing.Color")
comboBox1.Items.Add(prop.Name);
}
Any Suggestions or comments are appreciated.任何建议或意见表示赞赏。
Thanks!谢谢!
This worked for me.这对我有用。 Try a Debug.尝试调试。 You may be getting the colors but the problem is with is add items.您可能会得到 colors 但问题在于添加项目。
foreach (System.Reflection.PropertyInfo info in typeof(Colors).GetProperties())
{
Debug.WriteLine(info.Name);
}
You can import the style via a ResourceDictionary
您可以通过ResourceDictionary
导入样式
<ResourceDictionary Source="pack://application:,,,/PresentationFramework.Aero;V4.0.0.0;31bf3856ad364e35;component\themes/aero.normalcolor.xaml" />
And apply the style of the combo box.并应用组合框的样式。
Color
and not Colors
您的代码获得Color
的属性,而不是Colors
System.Windows.Media.Color
(instead of System.Drawing.Color
) colors 中的 class 是System.Windows.Media.Color
类型(而不是System.Drawing.Color
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.