tags:

views:

2260

answers:

3

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 own ComboBox using WPF and XAML markup?

A: 

There is a sample appliation that will be useful for you available to download from MSDN:

ColorPicker Custom Control Sample

This sample shows how to create a custom ColorPicker control and display it in a WPF dialog window.

Peter McGrattan
I don't need a full featured color picker. I saw that exanple, but it's not what I was looking for.
+4  A: 

Here is what I have done in a past ASP.net app:

// populate colors drop down (will work with other kinds of list controls)
Type colors = typeof(System.Drawing.Color);
PropertyInfo[] colorInfo = colors.GetProperties(BindingFlags.Public |
    BindingFlags.Static);
foreach ( PropertyInfo info in colorInfo)
{
    ddlColor.Items.Add(info.Name);                
}

// Get the selected color
System.Drawing.Color selectedColor = 
    System.Drawing.Color.FromName(ddlColor.SelectedValue);

Hope this helps!

Ronnie Overby
+12  A: 

Here is the pure XAML solution.

In your resources section, you would use this:

<ObjectDataProvider MethodName="GetType" 
    ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
    <ObjectDataProvider.MethodParameters>
        <sys:String>System.Windows.Media.Colors, PresentationCore,
            Version=3.0.0.0, Culture=neutral, 
            PublicKeyToken=31bf3856ad364e35</sys:String>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"  
    MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>

And then the combobox would look like this:

<ComboBox Name="comboBox1" 
    ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
    DisplayMemberPath="Name"
    SelectedValuePath="Name" />
casperOne
This is exactly what I was looking for ;] It works perfectly. Thank you very much for your time and help!
Go ahead and use your fancy-shmancy declarative programming! See if I care! JK :)
Ronnie Overby
@Ronnie Overby: Oh, I definitely get on with my bad self.
casperOne