I have created combo box that I would like to load with all of the standard colors. I would like to do this in the xaml.cs file rather than the straight XAML. I have found many examples to do this in the C# for .NET but not 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. But when i tried this it did not return any 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.
foreach (System.Reflection.PropertyInfo info in typeof(Colors).GetProperties())
{
Debug.WriteLine(info.Name);
}
You can import the style via a 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
System.Windows.Media.Color
(instead of System.Drawing.Color
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.