Show User Friendly Enum Value Into The Combobox

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.   
  7. public class EnumDescriptionConverter : IValueConverter  
  8.     {  
  9.         private string GetEnumDescription(Enum enumObj)  
  10.         {  
  11.             if (enumObj == null)  
  12.             {  
  13.                 return string.Empty;  
  14.             }  
  15.             FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());  
  16.   
  17.             object[] attribArray = fieldInfo.GetCustomAttributes(false);  
  18.   
  19.             if (attribArray.Length == 0)  
  20.             {  
  21.                 return enumObj.ToString();  
  22.             }  
  23.             else  
  24.             {  
  25.                 DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;  
  26.                 return attrib.Description;  
  27.             }  
  28.         }  
  29.   
  30.         object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)  
  31.         {  
  32.             Enum myEnum = (Enum)value;  
  33.             if (myEnum == null)  
  34.             {  
  35.                 return null;  
  36.             }  
  37.             string description = GetEnumDescription(myEnum);  
  38.             if (!string.IsNullOrEmpty(description))  
  39.             {  
  40.                 return description;  
  41.             }  
  42.             return myEnum.ToString();  
  43.         }  
  44.   
  45.         object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
  46.         {  
  47.             return string.Empty;  
  48.         }  
  49.     }  

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

Up Next
    Ebook Download
    View all
    Learn
    View all