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;