开发者

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. 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.


  1. Your code gets the properties of Color and not Colors
  2. The colors in that class are of type System.Windows.Media.Color (instead of System.Drawing.Color)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜