Databinding an enum property to a ComboBox in WPF
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Binding an enum to a ComboBox in WPF lets users select from a fixed set of options without hardcoding strings in XAML. The standard approach uses Enum.GetValues() to populate the ComboBox items and two-way data binding to sync the selected value with a view model property. For display-friendly names (e.g., "Dark Blue" instead of DarkBlue), you can use DescriptionAttribute with a custom converter or an ObjectDataProvider in XAML.
Define the Enum and ViewModel
The view model exposes both the selected value and the list of all enum values. Enum.GetValues() returns all members of the enum type as an array.
Bind in XAML
ItemsSource binds to the array of enum values. SelectedItem uses two-way binding by default for ComboBox, so selecting an item updates the view model property.
Using ObjectDataProvider (No ViewModel Property Needed)
ObjectDataProvider calls Enum.GetValues(typeof(ColorOption)) directly in XAML, so you do not need a ColorOptions property in the view model.
Display-Friendly Names with DescriptionAttribute
The ItemTemplate with the converter displays "Dark Blue" to the user while the bound SelectedItem remains the DarkBlue enum value.
Excluding Enum Values
Common Pitfalls
- ComboBox shows the enum member name (e.g.,
DarkBlue) instead of a friendly name: By default, WPF callsToString()on enum values. UseDescriptionAttributewith anIValueConverterin anItemTemplateto display human-readable names like "Dark Blue". - SelectedItem binding not updating the ViewModel: Ensure the ViewModel property type matches the enum type exactly. If the ComboBox
ItemsSourcecontainsobject[]fromEnum.GetValues()and the property is typed as the specific enum, the binding works. But if there is a type mismatch (e.g., binding to astringproperty), updates silently fail. - Forgetting INotifyPropertyChanged: Without raising
PropertyChangedin the setter, the UI does not update when the property changes programmatically. The ComboBox selection appears to work (user changes propagate to ViewModel), but ViewModel-initiated changes do not reflect in the UI. - ObjectDataProvider namespace errors:
ObjectDataProviderneedsxmlns:sys="clr-namespace:System;assembly=mscorlib"for theEnumtype. Missing this namespace causes a XAML compile error. In .NET Core / .NET 5+, useassembly=System.Runtimeinstead ofmscorlib. - Enum values changing at runtime: Enums are compile-time constants. If you need a dynamic set of options, use a collection of objects with a display name and value property instead of an enum. Enums are only appropriate for fixed, known sets of values.
Summary
- Expose
Enum.GetValues(typeof(MyEnum))as a ViewModel property or useObjectDataProviderin XAML - Bind
ItemsSourceto the enum values andSelectedItemto the ViewModel property - Use
DescriptionAttributewith anIValueConverterfor user-friendly display names - Use
ItemTemplatewith a converter to show descriptions while binding to enum values - Implement
INotifyPropertyChangedto keep the UI in sync with ViewModel changes - Filter enum values with LINQ in the ViewModel when certain options should be excluded
Related reading
- DataContractSerializer doesn't call my constructor?
- DataContractSerializer vs XmlSerializer Pros and Cons of each serializer
- DataGridView - how to set column width?
- DateTime vs DateTimeOffset
- DateTime vs DateTimeOffset
- DateTime.Compare how to check if a date is less than 30 days old?
- DateTime.Now vs. DateTime.UtcNow
- DateTime.Now.ToStringyyyy-MM-dd hhmmss is returning AM time instead of PM time?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.