Click here to Skip to main content
Rate this: bad
good
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")]
    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.
 
<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 1-Aug-12 12:06pm
Edited 1-Aug-12 12:22pm
v5
Comments
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
good
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[^]
  Permalink  
Rate this: bad
good
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.
 
Addition:
Another article is Binding and Using Friendly Enums in WPF[^], it is actually mentioned in the first article I listed.
  Permalink  
v2
Rate this: bad
good
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[^].
 
—SA
  Permalink  
Comments
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.
--SA
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...
--SA
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.
--SA
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...
--AS
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"?
--SA

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
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