Thursday, August 30, 2012

WPF – MVVM – Combobox binding with enums


WPF and MVVM is going smooth for me at least as of now. Today, I am going to disucss how combobx binding with enum can be done in MVVM pattern. Here I assume that, you have basic idea of MVVM and how it can be used with WPF applications. The MVVM explaination is out of scope for this blog post.
First of all create simple WPF application named as EnumToDDLBinding using VS2010. You can give any name as per your choice. If so then make sure that, you replace the namespace names in codes mentioned below with your namespace name.  Add a new folder named as “ViewModel”. Add a new class in it named as “MainWindowViewModel.cs. The add another class in solution and name it as “MyEnum.cs”.
 The overall structure of solution is as shown below –
 
Add sample enum to MyEnum.cs as shown below –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 


namespace EnumToDDLBinding
{
    public enum MyEnum
    {
        Enum0=0,
        Enum1=1,
        Enum2 = 2
    }
} 

Open the MainWindow.xaml code and add the combobox control as shown below –
<Window x:Class="EnumToDDLBinding.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1" ItemsSource="{Binding DDLItemSource, Mode=OneWay}" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>

Now note the ItemSource property above in XAML code. We have mentioned the binding property neme as DDLItemSource. Now we need to write this property in View model.
So the view model is as follows –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel; 

namespace EnumToDDLBinding.ViewModel
{
    public class MainWindowsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 

        private IEnumerable<MyEnum> ddlItemSource;
        public IEnumerable<MyEnum> DDLItemSource
        {
            get
            {
                return Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
            }
            set
            {
                ddlItemSource = value;
                RaisePropertyChanged("DDLItemSource");
            }
        }
    }
} 

Time to bind the view mode with view or XAML. Go to code behind of XAML file and add following line in consructor MainWindow() –

this.DataContext = new MainWindowsViewModel();

Run the application, and you should be able to see the enum values added in combobox as items –

 

Cheers…
Happy Binding!!

1 comment:

  1. i had gone through lots of sites this is the easist and simplest way i had found to bind the enum values to combo box thanks for the solution :)

    ReplyDelete