WPF
enum
combobox
data binding
tutorial

How to bind an enum to a combobox control in WPF?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Binding an enum to a WPF ComboBox is a clean way to present a fixed set of options without duplicating the values in XAML. The usual pattern is to expose the enum values as an ItemsSource and bind SelectedItem or SelectedValue to a ViewModel property of the same enum type.

Start with a Typed ViewModel

The ViewModel should hold the selected enum value, not a string. That keeps the UI and business logic aligned.

csharp
1using System.ComponentModel;
2using System.Runtime.CompilerServices;
3
4public enum Priority
5{
6    Low,
7    Medium,
8    High,
9    Critical
10}
11
12public class MainViewModel : INotifyPropertyChanged
13{
14    private Priority _selectedPriority = Priority.Medium;
15
16    public Priority SelectedPriority
17    {
18        get => _selectedPriority;
19        set
20        {
21            if (_selectedPriority != value)
22            {
23                _selectedPriority = value;
24                OnPropertyChanged();
25            }
26        }
27    }
28
29    public Array PriorityValues => Enum.GetValues(typeof(Priority));
30
31    public event PropertyChangedEventHandler? PropertyChanged;
32
33    private void OnPropertyChanged([CallerMemberName] string? name = null)
34    {
35        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
36    }
37}

PriorityValues provides a simple collection for the ComboBox to display. Because SelectedPriority is strongly typed, the selected item flows directly into application logic.

Bind the ComboBox in XAML

Once the DataContext is set to the ViewModel, the XAML is straightforward.

xml
1<Window x:Class="EnumBindingDemo.MainWindow"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Enum Binding Demo"
5        Height="180"
6        Width="300">
7    <StackPanel Margin="20">
8        <TextBlock Text="Priority" Margin="0,0,0,8" />
9        <ComboBox ItemsSource="{Binding PriorityValues}"
10                  SelectedItem="{Binding SelectedPriority, Mode=TwoWay}" />
11        <TextBlock Margin="0,12,0,0"
12                   Text="{Binding SelectedPriority}" />
13    </StackPanel>
14</Window>

And in the window code-behind:

csharp
1public partial class MainWindow : Window
2{
3    public MainWindow()
4    {
5        InitializeComponent();
6        DataContext = new MainViewModel();
7    }
8}

This is enough for many internal tools and admin screens.

User-Friendly Display Names

Raw enum names are often acceptable for prototypes, but production UI labels usually need better formatting. A common solution is to apply DescriptionAttribute or DisplayAttribute and then use a converter or helper to show a friendly label.

For example, if the enum contains InProgress, the UI may need "In Progress". The underlying value should remain the enum member, while the display text becomes presentation-specific.

Another lightweight option is to name the enum members exactly as you want them displayed, but that works only when code naming and UI naming can safely match.

Why Binding Beats Manual Item Lists

You could hard-code ComboBoxItem elements in XAML, but that creates duplication and increases the risk of mismatched values. Binding from the enum keeps one source of truth. If a new enum member is added later, the list updates automatically.

That also improves testability. The ViewModel can be validated independently from the WPF view, and the selected value remains a typed enum instead of an arbitrary string.

Common Pitfalls

  • Binding the ComboBox to strings instead of enum values forces extra parsing later. Bind to the enum type directly when possible.
  • Forgetting Mode=TwoWay can stop the selected value from updating the ViewModel. Use explicit two-way binding for editable selections.
  • Exposing Enum.GetValues incorrectly can produce an awkward collection type in older code. Returning an Array or a strongly typed list avoids confusion.
  • Using raw enum member names in a public-facing UI often produces poor labels. Add display metadata or a converter when readability matters.
  • Setting the wrong DataContext makes the binding appear broken even though the XAML is correct. Verify the view is bound to the intended ViewModel instance.

Summary

  • In WPF, bind a ComboBox to the enum values through ItemsSource.
  • Bind SelectedItem to a ViewModel property of the enum type.
  • A typed ViewModel is cleaner than string-based parsing.
  • Display metadata can improve labels without changing the underlying enum values.
  • Enum binding keeps the UI synchronized with the domain model and reduces duplication.

Course illustration
Course illustration

All Rights Reserved.