How can I list colors in WPF with XAML?

前端 未结 4 903
耶瑟儿~
耶瑟儿~ 2020-12-05 08:14

How can I get list of all colors I can pick in Visual Studio Designer (which is System.Windows.Media.Colors, but that isn\'t a collection) and put them into my

4条回答
  •  温柔的废话
    2020-12-05 08:30

    Here's how to do it in code using reflection. The following will dump all predefined WPF color names to Output:

    using System.Reflection;
    
    void ListAllColors()
    {
        Type colorsType = typeof(System.Windows.Media.Colors);
        PropertyInfo[] colorsTypePropertyInfos = colorsType.GetProperties(BindingFlags.Public | BindingFlags.Static);
    
        foreach (PropertyInfo colorsTypePropertyInfo in colorsTypePropertyInfos)
            Debug.WriteLine(colorsTypePropertyInfo.Name);
    }
    

    And to put them in a combobox, you could simply change the last line to:

    _comboBox.Items.Add(colorsTypePropertyInfo.Name); 
    

提交回复
热议问题