Any real world appliation would like bind the Enum values to a ComboBox. So it is always better to have common code that will do the logic. Below, I have a helper class which exposed a property to get the enum type, so that I can resolve the enum values.

   1: public static Type GetEnum(DependencyObject obj)

   2:         {

   3:             return (Type)obj.GetValue(EnumProperty);

   4:         }

   5:  

   6:         public static void SetEnum(DependencyObject obj, string value)

   7:         {

   8:             obj.SetValue(EnumProperty, value);

   9:         }

  10:  

  11:         // Using a DependencyProperty as the backing store for Enum.  This enables animation, styling, binding, etc...

  12:         public static readonly DependencyProperty EnumProperty =

  13:             DependencyProperty.RegisterAttached("Enum", typeof(Type), typeof(EnumHelper), new PropertyMetadata(null, OnEnumChanged));

Get the values of enum and set it as ComboBox ItemsSource.

   1: private static void OnEnumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

   2:         {

   3:             var control = sender as ItemsControl;

   4:  

   5:             if (control != null)

   6:             {

   7:                 if (e.NewValue != null)

   8:                 {

   9:                     var _enum = Enum.GetValues(e.NewValue as Type);

  10:                     control.ItemsSource = _enum;

  11:                 }

  12:             }

  13:         }

Now the ComboBox will bind to the enum values, if we set the enum value in XAML.

   1: public enum Designation

   2:     {

   3:         SoftwareEngineer,

   4:  

   5:         TeamLead,

   6:  

   7:         ProductManager

   8:     }

   1: <ComboBox x:Name="Designation" Margin="5" Grid.Row="5"

   2:                          SelectedItem="{Binding Designation, Mode=TwoWay}"

   3:                          local:EnumHelper.Enum="{x:Type local:Designation}">

   4:                   

   5: </ComboBox>

image

Look at the names of every item. It is not well spaced. It should be Product Manager instead of ProductManager. For this we will use the Display attribute to enum values. Further we can also use Description attribute to let the user see more details.

I have modified my enum looks below.

   1: public enum Designation

   2:     {

   3:         [Display(Name="Software Engineer")]

   4:         [Description("Software engineer responsible for core developement.")]

   5:         SoftwareEngineer,

   6:  

   7:         [Display(Name = "Team Lead")]

   8:         [Description("Team lead responsible for leading a small team of 5 to 10 members.")]

   9:         TeamLead,

  10:  

  11:         [Display(Name = "Product Manager")]

  12:         [Description("Product manager responsible for core management.")]

  13:         ProductManager

  14:     }

Also our helper class should expose another property to get the values of attributes and set it wherever needed. For example, in this case I will show the description as tooltip and display as text.

   1: public static bool GetMoreDetails(DependencyObject obj)

   2:         {

   3:             return (bool)obj.GetValue(MoreDetailsProperty);

   4:         }

   5:  

   6:         public static void SetMoreDetails(DependencyObject obj, bool value)

   7:         {

   8:             obj.SetValue(MoreDetailsProperty, value);

   9:         }

  10:  

  11:         // Using a DependencyProperty as the backing store for MoreDetails.  This enables animation, styling, binding, etc...

  12:         public static readonly DependencyProperty MoreDetailsProperty =

  13:             DependencyProperty.RegisterAttached("MoreDetails", typeof(bool), typeof(EnumHelper), new PropertyMetadata(false, OnMoreDetailsChanged));

  14:  

  15:         private static void OnMoreDetailsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)

  16:         {

  17:             var control = sender as FrameworkElement;

  18:             if (control != null)

  19:             {

  20:                 var enumobject = control.DataContext;

  21:                 var fieldInfo = enumobject.GetType().GetField(enumobject.ToString());

  22:  

  23:                 var array = fieldInfo.GetCustomAttributes(false);

  24:  

  25:                 if (array.Length == 0)

  26:                 {

  27:                     if (control is TextBlock)

  28:                     {

  29:                         ((TextBlock)control).Text = enumobject.ToString();

  30:                     }

  31:                     else if (control is ContentControl)

  32:                     {

  33:                         ((ContentControl)control).Content = enumobject;

  34:                     }

  35:                     return;

  36:                 }

  37:  

  38:                 foreach (var o in array)

  39:                 {

  40:                     if (o is DescriptionAttribute)

  41:                     {

  42:                         control.ToolTip = ((DescriptionAttribute) o).Description;

  43:                     }

  44:                     else if (o is DisplayAttribute)

  45:                     {

  46:                         if (control is TextBlock)

  47:                         {

  48:                             ((TextBlock) control).Text = ((DisplayAttribute) o).Name;

  49:                         }

  50:                         else if (control is ContentControl)

  51:                         {

  52:                             ((ContentControl)control).Content = ((DisplayAttribute)o).Name;

  53:                         }

  54:                     }

  55:                 }

  56:             }

  57:         }

Let’s add a simple DataTemplate to our ComboBox.

   1: <ComboBox x:Name="Designation" Margin="5" Grid.Row="5"

   2:                           SelectedItem="{Binding Designation, Mode=TwoWay}"

   3:                           local:EnumHelper.Enum="{x:Type local:Designation}">

   4:                     <ComboBox.ItemTemplate>

   5:                         <DataTemplate>

   6:                             <TextBlock local:EnumHelper.MoreDetails="true"/>

   7:                         </DataTemplate>

   8:                     </ComboBox.ItemTemplate>

   9: </ComboBox>

image

Download EnumHelper.zip