Bind Combobox With Enum Using ObjectDataProvider WPF MVVM

Today, in this article I will explain how to bind combo boxes with enum directly using ObjectDataProvider in WPF MVVM.

Instead of programmatically binding, we will use ObjectDataProvider.

ObjectDataProvider

It enables you to create your object in XAML and make it available as a binding source. Available under System.Window.Data Namespace.

Let us understand how it works in the following steps.

Step 1. Create a static class called “EnumClass” containing an Enum called “Positions” as mentioned below.

public static class EnumClass  
{  
    public enum Positions  
    {  
        Fresher = 1,  
        EntryLevel = 2,  
        Junior = 3,  
        MidLevel = 4,  
        Senior = 5,  
        TeamLead = 6,  
        ProjectManager = 7  
    };  
}  

Step 2. Now prepare a WPF window with one combo box as shown below.

WPF window

Step 3. Now move to the App.xaml file to declare ObjectDataProvider to prepare an object in xaml to use as a binding source for combobox.

First Add the following namespace to App.xaml page.

xmlns:System="clr-namespace:System;assembly=mscorlib"

This namespace is required to access objects defined under System namespace.

Then add a namespace for the current project to access objects defined in the current project.

xmlns:enm="clr-namespace:BindComboboxToEnum"

Here BindComboboxToEnum is the name of my project.

Then inside the Application. Resources tag, declare ObjectDataProvider as shown below.

<Application.Resources>  
    <ObjectDataProvider x:Key="enmPositions" MethodName="GetValues" ObjectType="{x:Type System:Enum}">  
        <ObjectDataProvider.MethodParameters>  
            <x:Type TypeName="enm:EnumClass+Positions"></x:Type>  
        </ObjectDataProvider.MethodParameters>  
    </ObjectDataProvider>  
</Application.Resources>  
  • impositions identification key to access declared ObjectDataProvider across the application.
  • GetValues is the method name required to get the values from the enum. Please make sure that MethodName may be different based on user data type. It's related to the same method name we are using to get the values from the object. To get values from Enum, we are using the GetValues method which is why we have used it here.

As you can see, here we have provided a declared enum as a method provider so that an object will be created with values declared under that enum.

Here we have used EnumClass+Positions, that is because our Enum declared inside the Static class EnumClass.

For some of you, ‘+’ may be new. Guys, that is used for nested binding. If you want to bind an object declared inside an object then in XAML you can go with this pattern.

We have declared ObjectDataProvider in App.xaml so that it can be accessed in any of the XAML files inside the project.

Step 4. Now, bind the declared ObjectDataProvider with the combo box as mentioned below.

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

The complete XAML implementation for our window will be like below.

<Window x:Class="BindComboboxToEnum.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BindComboboxToEnum"
        mc:Ignorable="d"
        Title="MainWindow" Height="100" Width="300">
    <Grid>
        <StackPanel>
            <Label Content="Positions" Margin="10,0,0,0" Height="25"></Label>
            <ComboBox Height="25" ItemsSource="{Binding Source={StaticResource enmPositions}}" Margin="10,2,10,0"></ComboBox>
        </StackPanel>
    </Grid>
</Window>

Run the application and you can see that our declared Enum is directly bound with combobox.

WPF

In my next article, we will learn how to show user-friendly enum names inside the bound combo box.