Click here to Skip to main content
15,909,621 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help!

Using .net 4.0 and WPF I have a property Grid that is bounded to a class and all its properties. One of the properties of the class is of type enum like:
C#
public enum ColorChoices
{
    [Description("My favorite color - Red")]
    Red,
    [Description("My second favorite color - Black")]
    Black,
   [Description("My third favorite color - White")]
    White
}

It shows up on the UI as a drop down with choices of Red, Black, White. How do I get the UI to use the custom descriptions defined in Description?

Thanks

Here is my binding. I just bound the propertyGrid to the class preferences which has many properties, one of which is the enum ColorChoices above. I dont specifically bind to a combobox. Just to the propertygrid.

XML
<propGrid:PropertyGrid
    Grid.Column="0"
    Width="Auto"
    MinWidth="200"
    SelectedObject="{Binding Path=Preferences}"
    AdvancedOptionsMenu="{x:Null}"
    IsCategorized="True"
    ShowSearchAndOrderOptions="True"
    DisplaySummary="True">
    <propGrid:PropertyGrid.EditorDefinitions>
        <propGrid:EditorDefinition
            TargetType="{x:Type questinfra:SerializableColor}">
            <propGrid:EditorDefinition.EditorTemplate>
                <DataTemplate>
                    <propGridControls:ColorPicker
                        DisplayColorAndName="True"
                        SelectedColor="{Binding Value, Mode=TwoWay, Converter={StaticResource SerializableColorToColorConverter}}" />
                </DataTemplate>
            </propGrid:EditorDefinition.EditorTemplate>
        </propGrid:EditorDefinition>
    </propGrid:PropertyGrid.EditorDefinitions>
</propGrid:PropertyGrid>
Posted
Updated 1-Aug-12 11:22am
v5
Comments
Kenneth Haugland 1-Aug-12 17:11pm    
And how does your binding in XAML look like? You could just design a celltemplate that binds to you Enum while the combobox binds to the colors.
Hamid Moradi 1-Aug-12 17:41pm    
Thank you for your reply> I added my binding to the original question. I dont bind to a combo box. I bind the entire class that has many properties to the PropertyGrid.
Kenneth Haugland 1-Aug-12 18:14pm    
I think your question is answer, dont you? ;)
Hamid Moradi 3-Aug-12 17:47pm    
The answer might be there but I still dont see it! Every example or article I read talks about how to bound enums to a combo box and get friendly names from it. The article below talks about how to do it in the code without the UI. I have a WPF property grid (have to use it!) that is bounded to a class where some members of the class are enums are some are strings and some are integers. I can not replace the property grid with a combo box. Have to be able to tell the property grid to use the freindly name for the enum whereever the property is an enum. Am I missing something from the replies below?

I have a very general article on the problem of human-readable names for .NET enumeration members, with very universal approach. Please read it:
Human-readable Enumeration Meta-data[^].

—SA
 
Share this answer
 
Comments
Kenneth Haugland 1-Aug-12 18:05pm    
Thats windows Forms no? I could tell by the nasty colors ;)
Sergey Alexandrovich Kryukov 1-Aug-12 18:13pm    
Of course not. (Who do you think I am to waste time on tightly couple stuff and lock-ins?) Absolutely universal code, not related to UI. Demo for Forms and WPF, .NET Framework 2.0, 3.5, 4.0 and Mono.
--SA
Kenneth Haugland 1-Aug-12 18:16pm    
Oh... You buildt it for everytthing... My bad... :-/
Sergey Alexandrovich Kryukov 1-Aug-12 18:19pm    
No problem. This is a cycle of 3 articles on enumerations, based on making enumeration... enumerate, pretty interesting, most readers said...
--SA
Sergey Alexandrovich Kryukov 1-Aug-12 18:15pm    
And nasty color was default XP, totally tasteless. I always switched to old not-so-good-but-not-too-bad classical W2K style, before Windows 7, which is much better.
--SA
Have a look at this article: Displaying User Friendly Enum Values in WPF[^]
It has a section called Text customization using code attributes.

Addition:
Another article is Binding and Using Friendly Enums in WPF[^], it is actually mentioned in the first article I listed.
 
Share this answer
 
v2
Your question has a full demo here:
Step by Step WPF Data Binding with Comboboxes[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900