Click here to Skip to main content
12,951,971 members (62,248 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 26 Apr 2013

Enum to ComboBox binding

, 7 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip gives a simple way to bind enum values into a ComboBox that could be reused in different applications.


Any real world application would like to bind Enum values to a ComboBox. So it is always better to have a common code that will do the logic.

Using the Code

I have a helper class which exposes a property to get the enum type so that I can resolve the enum values.

public static Type GetEnum(DependencyObject obj)
    return (Type)obj.GetValue(EnumProperty);
public static void SetEnum(DependencyObject obj, string value)
    obj.SetValue(EnumProperty, value);
// Using a DependencyProperty as the backing store for Enum.  
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnumProperty =
    DependencyProperty.RegisterAttached("Enum", typeof(Type), 
    typeof(EnumHelper), new PropertyMetadata(null, OnEnumChanged));  

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

private static void OnEnumChanged(DependencyObject sender, 
                          DependencyPropertyChangedEventArgs e)
    var control = sender as ItemsControl;
    if (control != null)
        if (e.NewValue != null)
            var _enum = Enum.GetValues(e.NewValue as Type);
            control.ItemsSource = _enum;

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

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

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

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

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

I have modified my enum to look like below:

public enum Designation
    [Display(Name="Software Engineer")]
    [Description("Software engineer responsible for core development.")]
    [Display(Name = "Team Lead")]
    [Description("Team lead responsible for leading a small team of 5 to 10 members.")]
    [Display(Name = "Product Manager")]
    [Description("Product manager responsible for core management.")]

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 a tooltip and display as text.

public static bool GetMoreDetails(DependencyObject obj)
    return (bool)obj.GetValue(MoreDetailsProperty);
public static void SetMoreDetails(DependencyObject obj, bool value)
    obj.SetValue(MoreDetailsProperty, value);
// Using a DependencyProperty as the backing store for MoreDetails.  
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty MoreDetailsProperty =
    typeof(bool), typeof(EnumHelper), new PropertyMetadata(false, OnMoreDetailsChanged)); 

We are getting the attribute values and set it in the appropriate places. We expect the user to set this property in any DataTemplate so that we can get the underlying enum object as a DataContext.

var array = fieldInfo.GetCustomAttributes(false);
if (array.Length == 0)
    if (control is TextBlock)
        ((TextBlock)control).Text = enumobject.ToString();
    else if (control is ContentControl)
        ((ContentControl)control).Content = enumobject;
foreach (var o in array)
    if (o is DescriptionAttribute)
        control.ToolTip = ((DescriptionAttribute) o).Description;
    else if (o is DisplayAttribute)
        if (control is TextBlock)
            ((TextBlock) control).Text = ((DisplayAttribute) o).Name;
        else if (control is ContentControl)
            ((ContentControl)control).Content = ((DisplayAttribute)o).Name;

Let's add a DataTemplate to the ComboBox:

    <TextBlock local:EnumHelper.MoreDetails="True"/>


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jawahar Suresh Babu
Software Developer (Senior)
India India
Jawahar working as a Senior Development Engineer in Aditi Technologies,Bangalore, India. Specialist in all XAML frameworks. Very passionate on UX Design and Development. Skilled in Expression Blend, Design, WPF, Silverlight, Windows Phone 7/8, Windows 8. Good knowledge in Entity Framework, SQLite and SQL Server also. Also had good experience with PRISM, MVVM, Caliiburn Micro and other design patterns.

He developed few products for Syncfusion Inc. Also working on some freelancing projects. Worked as a lead developer of Metro Studio from Syncfusion Inc.

An active freelancer.

You may also be interested in...

Comments and Discussions

QuestionMessage Automatically Removed Pin
7-Nov-15 14:13
memberMember 121225627-Nov-15 14:13 
QuestionNeat idea Pin
Kenneth Haugland18-Jun-15 11:40
professionalKenneth Haugland18-Jun-15 11:40 
QuestionSimilar method Pin
drussilla27-Jan-15 4:07
memberdrussilla27-Jan-15 4:07 
GeneralMy vote of 1 Pin
Member 988567418-Jan-15 21:52
memberMember 988567418-Jan-15 21:52 
GeneralMy vote of 1 Pin
Jalal Khordadi10-Dec-14 3:21
memberJalal Khordadi10-Dec-14 3:21 
Bugbox doesn't refresh value Pin
negstek21-Oct-14 23:18
membernegstek21-Oct-14 23:18 
GeneralRe: box doesn't refresh value Pin
negstek22-Oct-14 22:45
membernegstek22-Oct-14 22:45 
GeneralMy vote of 1 Pin
Member 103920786-Oct-14 0:26
memberMember 103920786-Oct-14 0:26 
GeneralRe: My vote of 1 Pin
caronjudith7-Oct-14 4:44
membercaronjudith7-Oct-14 4:44 
Questionnice helper Pin
negstek25-Sep-14 2:16
membernegstek25-Sep-14 2:16 
GeneralMy vote of 1 Pin
Member 104580688-Aug-14 5:54
memberMember 104580688-Aug-14 5:54 
GeneralRe: My vote of 1 Pin
negstek25-Sep-14 2:34
membernegstek25-Sep-14 2:34 
QuestionBug Pin
m23tran15-Apr-14 11:23
memberm23tran15-Apr-14 11:23 
SuggestionRe: Bug Pin
FormatException11-Jul-14 4:57
memberFormatException11-Jul-14 4:57 
GeneralMy vote of 5 Pin
Андрей Москвичёв27-Nov-13 4:41
professionalАндрей Москвичёв27-Nov-13 4:41 
QuestionProblem Pin
Member 383386223-Sep-13 3:00
memberMember 383386223-Sep-13 3:00 
AnswerRe: Problem Pin
Member 99273029-Jan-14 4:50
memberMember 99273029-Jan-14 4:50 
GeneralRe: Problem Pin
Andreas Wittberg9-Apr-14 4:58
memberAndreas Wittberg9-Apr-14 4:58 
QuestionI'm getting an error Pin
Shane Montgomery20-Aug-13 12:03
memberShane Montgomery20-Aug-13 12:03 
AnswerRe: I'm getting an error Pin
themightylechuck1-Sep-13 18:09
memberthemightylechuck1-Sep-13 18:09 
GeneralRe: I'm getting an error Pin
Shane Montgomery2-Sep-13 4:26
memberShane Montgomery2-Sep-13 4:26 
GeneralMy vote of 5 Pin
Gary M. Armstrong6-Aug-13 4:17
memberGary M. Armstrong6-Aug-13 4:17 
QuestionRocks! Pin
Gary M. Armstrong6-Aug-13 4:17
memberGary M. Armstrong6-Aug-13 4:17 
GeneralMy vote of 5 Pin
christoph brändle15-May-13 3:54
memberchristoph brändle15-May-13 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 7 May 2013
Article Copyright 2013 by Jawahar Suresh Babu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid