Infosys Microsoft Alliance and Solutions blog

« Hiding from Debugger | Main | Silverlight 2.0 RC0 Installation »

WPF - Displaying enums in ComboBox control

Displaying enums in ComboBox in WPF isn't really rocket science. The issue however comes when you want to display user friendly strings against the enum values. Enum unfortunately doesn't support this by a simple override to the ToString method.

The usual approach people take is the use the DescriptionAttribute. This attribute is assigned to each enum value and at runtime, using reflection, the value of the attribute is queried and is then displayed. The approach is definitely worth considering and I found an implementation of the same and a custom WPF ComboBox that will display such user friendly enum strings here.

However reflection does mean a bit of performance impact and depending on the size of the enum, this can vary. In the above implementation, the values are hence cached for any reuse and improve efficiency. However if the binding happens only once, this has no effect.

I was hence wondering on how to do this without having to use reflection. I then hit upon an idea of using a dictionary object with the enum as the key and the value as the user friendly string. It was then a matter of binding this with the ComboBox and set the DisplayMemberPath appropriately. In order to ensure that the enum and this dictionary collection go hand in hand, I decided to encapsulate them in a single class. There is definitely a maintenance overhead of ensuring that appropriate key value pair is added to the dictionary for each enum. See the code for this class below.

    internal class Utility


        static Utility()


            //initialize the collection with user friendly strings for each enum

            _monthCol = new Dictionary<Months, string>(){

                {Months.Jan, "January"},

                {Months.Feb, "February"},

                {Months.Mar, "March"},

                {Months.Apr, "April"},

                {Months.May, "May"},

                {Months.Jun, "June"},

                {Months.Jul, "July"},

                {Months.Aug, "August"},

                {Months.Sep, "September"},

                {Months.Oct, "October"},

                {Months.Nov, "November"},

                {Months.Dec, "December"}};




        public enum Months
















        private static Dictionary<Months, string> _monthCol;

        public static Dictionary<Months, string> MonthCollection




                return _monthCol;




and in XAML, this Utility class can be instantiated as a resource and then referenced in the ComboBox creation.  

            <ComboBox ItemsSource="{Binding Source={StaticResource monthCollection}, Path=MonthCollection}" SelectedIndex="0" DisplayMemberPath="Value" SelectedValuePath="Key" />

Note that the SelectedValuePath property has also been set to point to the Key in the dictionary. with this, it is easy to handle the selection changed event and the combobox's SelectedValue property will then be the enum value and you can then easily use a switch/case block to handle it appropriately. If  you don't do it, you can still easily cast this to get to the enum value as below

            switch(((KeyValuePair<Utility.Months, string>)cmbBox.SelectedValue).Key)


                //your case statements here


                    //custom logic



 Comments welcome !





Very nice approach, Atul.
I'm doing basically the same thing but, in a different way.

Fabrizio, Thanks. Would you like to share how you are doing this?

I'd be curious to see your profile results showing that Reflection was actually causing a significant performance hit. I wouldn't have expected it to even be noticeable compared to the overhead of rendering all the GUI controls.

This is a very nice solution. I noticed one weird thing though:
When I open the combo box each item in the list shows this:
[enumvalue, friendly text]

I changed the xaml by removing the DisplayMemberPath attribute and adding the following ItemTemplate that had only a TextBlock with the text bound to Value and that solved the strange display issue.

The selected item in the combobox showed fine in both situations, by the way.

If it displays [enumvalue, friendly text], you need to use ComboBox.SelectedValue instead of ComboBox.SelectedItem.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter