Binding a Combobox to an Enum in C# and WPF using XAML and MVVM

I saw a lot of posts on how to bind an enum to a combobox using code behind and C#, but since I was using MVVM, I needed a different solution. I basically wanted to bind a list of the enums in a maintenance view model, and have the the xaml handle the binding of the item list. To help remember what I did and help whomever along the way with the same issue, I thought I would write about it.

Let’s start off with a basic enumeration :

1public enum VolumeLevel
2{
3     [Description("Low")]
4     LowVolume = 1,
5     [Description("Medium")]
6     MediumVolume = 3,
7     [Description("High")]
8     HighVolume = 5
9}

Now make that enumeration into a collection in your maintenance view model (or where ever you bind the lists on your comboboxes, etc…). There are a few ways to do this, I just replicated what I’ve done with other collections, just with a little twist to get the descriptions.

1private List<keyvaluepair<string,<span class="hiddenSpellError">VolumeLevel>> _VolumeLevelList;
2public List<keyvaluepair<string,<span class="hiddenSpellError">VolumeLevel>> VolumeLevelList
3{
4      get
5      {
6         if (_VolumeLevelList == null)
7         {
8         _VolumeLevelList = new List<KeyValuePair<string, VolumeLevel>>();
9         foreach (VolumeLevel level in Enum.GetValues(typeof(VolumeLevel)))
10         {
11             string Description;
12             FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
13             DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
14             if (attributes != null && attributes.Length > 0) { Description = attributes[0].Description; }
15             else { Description = string.Empty; }
16             KeyValuePair<string, volumelevel=""> TypeKeyValue =
17             new KeyValuePair<string, VolumeLevel>(Description, level);
18             _VolumeLevelList.Add(TypeKeyValue);
19         }
20      }
21      return _VolumeLevelList;
22   }
23}


So now that you have your list and your enumeration, let’s insert the final piece. This will be the combobox in the xaml with bindings to your List. Note : The form I am using is a child of a maintenance view, so if your enum list is within the current datacontext, you won’t need the relative source binding.

1<ComboBox
2Height="23"
3Width="125"
4HorizontalAlignment="Left"
5DisplayMemberPath="Key"
6SelectedValuePath="Value"
7ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:VolumeChangerMaintenanceView}}, Path=DataContext.VolumeLevelList}"
8SelectedValue="{Binding SelectedVolumeLevel, ValidatesOnDataErrors=True, Mode=TwoWay}"
9/>

There you go. All wired up and good to go. There are a few different ways to do this, but I thought I’d share mine just in case it helps somebody. Happy coding!

** UPDATE 11/16/2012 **
George had asked for a working example. I noticed an error in my code and is now fixed. Here is the working example!

**UPDATE 1/9/2014**
There was an issue with not calling the raise property changed from the public property, code has been updated and fixed

Jacob Saylor

Software developer in Kentucky

3 Responses

  1. Wonderful! This is the first example that has worked for me. I’ve been on this issue for a few days now, and whoever designed ComboBox should be punished.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: