Click here to Skip to main content
11,707,470 members (70,953 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF Enum
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:
public enum ColorChoices
    [Description("My favorite color - Red")]
    [Description("My second favorite color - Black")]
   [Description("My third favorite color - 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?


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.

    SelectedObject="{Binding Path=Preferences}"
            TargetType="{x:Type questinfra:SerializableColor}">
                        SelectedColor="{Binding Value, Mode=TwoWay, Converter={StaticResource SerializableColorToColorConverter}}" />
Posted 1-Aug-12 11:06am
Edited 1-Aug-12 11:22am
Kenneth Haugland at 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 at 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 at 1-Aug-12 18:14pm
I think your question is answer, dont you? ;)
Hamid Moradi at 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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Your question has a full demo here:
Step by Step WPF Data Binding with Comboboxes[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Have a look at this article: Displaying User Friendly Enum Values in WPF[^]
It has a section called Text customization using code attributes.

Another article is Binding and Using Friendly Enums in WPF[^], it is actually mentioned in the first article I listed.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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[^].

Kenneth Haugland at 1-Aug-12 18:05pm
Thats windows Forms no? I could tell by the nasty colors ;)
Sergey Alexandrovich Kryukov at 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.
Kenneth Haugland at 1-Aug-12 18:16pm
Oh... You buildt it for everytthing... My bad... :-/
Sergey Alexandrovich Kryukov at 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...
Sergey Alexandrovich Kryukov at 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.
Kenneth Haugland at 1-Aug-12 18:18pm
Wondered if you put it throuh Instagram (the polaroyd program for normal pictures) .... Pyuck.. (means what my face looks like when Im throwing up like color)
Sergey Alexandrovich Kryukov at 1-Aug-12 18:21pm
What is that Instagram, I wonder? I'm quite interested in stuff like industrial design and have some "pro" background in color measurements, etc...
Kenneth Haugland at 1-Aug-12 18:24pm
Hah... You should ask a new Question... Facebook paid a billion for the company I think...
Sergey Alexandrovich Kryukov at 1-Aug-12 18:43pm
Ah, "problem sharing, reinvented"? No problem, I'll read about it. Why would one put it "throw"?

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

  Print Answers RSS
0 OriginalGriff 156
1 Sergey Alexandrovich Kryukov 145
2 Mika Wendelius 140
3 chainerlt 110
4 ppolymorphe 104
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,806
4 Mika Wendelius 3,706

Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100