In my previous article, we learned how to bind a combobox directly with the enum. However, while binding a combobox with enum, one can face a common problem that enum values are not user-friendly for display. So here, we will learn a way to show user-friendly enum names to end users.

Please note that here, I have considered the implementation mentioned in the previous article, so please check that first before going further.

Step1

Update “EnumClass” as mentioned below.

  1. using System.ComponentModel;
  2. public static class EnumClass
  3. {
  4. public enum Positions
  5. {
  6. Fresher = 1,
  7. [Description("Entry Level")]
  8. EntryLevel = 2,
  9. Junior = 3,
  10. [Description("Mid Level")]
  11. MidLevel = 4,
  12. Senior = 5,
  13. [Description("Team Lead")]
  14. TeamLead = 6,
  15. [Description("Project Manager")]
  16. ProjectManager = 7
  17. };
  18. }

As you can see here, we have added user-friendly description text for some of the enum values.

For Example, “EntryLevel” text is less user-Friendly then “Entry Level” for display.

Here, we have to add reference of “System.ComponentModel” namespace into the EnumClass File to define “Description” attribute above enum value.

Step 2

Now, we will implement IValueConverter to display enum description instead of enum value. For that, create a class called EnumDescriptionConverter as mentioned below.

  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using System.Windows.Data;
  6. public class EnumDescriptionConverter : IValueConverter
  7. {
  8. private string GetEnumDescription(Enum enumObj)
  9. {
  10. if (enumObj == null)
  11. {
  12. return string.Empty;
  13. }
  14. FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
  15. object[] attribArray = fieldInfo.GetCustomAttributes(false);
  16. if (attribArray.Length == 0)
  17. {
  18. return enumObj.ToString();
  19. }
  20. else
  21. {
  22. DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
  23. return attrib.Description;
  24. }
  25. }
  26. object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
  27. {
  28. Enum myEnum = (Enum)value;
  29. if (myEnum == null)
  30. {
  31. return null;
  32. }
  33. string description = GetEnumDescription(myEnum);
  34. if (!string.IsNullOrEmpty(description))
  35. {
  36. return description;
  37. }
  38. return myEnum.ToString();
  39. }
  40. object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  41. {
  42. return string.Empty;
  43. }
  44. }

We are going to use this converter with binding. In addition, this converter will return description string to bound control instead of original enum value if defined.

Step 3

Now, we will move to XAML part to apply this converter with binding to display description instead of original enum value. First, we will add reference of implemented IValueConverter as a resource as mentioned below to use it in XAML file.

  1. <Window.Resources>
  2. <local:EnumDescriptionConverter x:Key="descConverter"></local:EnumDescriptionConverter>
  3. </Window.Resources>

Then as per the previous article, we have bound combobox with enum values as mentioned below.

  1. <ComboBox Height="25" ItemsSource="{Binding Source={StaticResource enmPositions}}" Margin="10,2,10,0"></ComboBox>

Now, we will add converter with ItemSource as mentioned below,

  1. <ComboBox Height="25" ItemsSource="{Binding Source={StaticResource enmPositions}}" Margin="10,2,10,0">
  2. <ComboBox.ItemTemplate>
  3. <DataTemplate>
  4. <TextBlock Text="{Binding Converter={StaticResource descConverter}}"></TextBlock>
  5. </DataTemplate>
  6. </ComboBox.ItemTemplate>
  7. </ComboBox>

That is it; we are done with the implementation. Run the project and see the result. It should be as mentioned in the below screenshot.

WPF