Bind Combobox With Enum Using ObjectDataProvider WPF MVVM

Today, in this article I will explain how to bind combo box 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.

  1. public static class EnumClass  
  2.     {  
  3.         public enum Positions  
  4.         {  
  5.             Fresher = 1,  
  6.             EntryLevel = 2,  
  7.             Junior = 3,  
  8.             MidLevel = 4,  
  9.             Senior = 5,  
  10.             TeamLead = 6,  
  11.             ProjectManager = 7  
  12.         };  
  13.     }  

Step 2

Now prepare a WPF window with one combobox as shown below.

WPF

Step 3

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

First Add 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 Application.Resources tag, declare ObjectDataProvider as shown below.

  1. <Application.Resources>  
  2.         <ObjectDataProvider x:Key="enmPositions" MethodName="GetValues" ObjectType="{x:Type System:Enum}">  
  3.             <ObjectDataProvider.MethodParameters>  
  4.                 <x:Type TypeName="enm:EnumClass+Positions"></x:Type>  
  5.             </ObjectDataProvider.MethodParameters>  
  6.         </ObjectDataProvider>  
  7.     </Application.Resources>  
  • enmPositions 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 GetValues method that is why we have used it here.

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

Here we have used EnumClass+Positions, that is because our Enum declared inside 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 declared ObjectDataProvider with combo box as mentioned below.

 

  1. <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.

  1. <Window x:Class="BindComboboxToEnum.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.         xmlns:local="clr-namespace:BindComboboxToEnum"  
  7.         mc:Ignorable="d"  
  8.         Title="MainWindow" Height="100" Width="300">  
  9.     <Grid>  
  10.         <StackPanel>  
  11.             <Label Content="Positions" Margin="10,0,0,0" Height="25"></Label>  
  12.             <ComboBox Height="25" ItemsSource="{Binding Source={StaticResource enmPositions}}" Margin="10,2,10,0"></ComboBox>  
  13.         </StackPanel>  
  14.     </Grid>  
  15. </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.