Thursday, 12 February 2009

How to bind enum to Combo Box in WPF

I was working one of my projects which contain implementation of View Model concept in WPF and this is the implementation from that project a very small and simple example.

First of all need to add references which are:

<Window ……………

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

xmlns:n="……Name space of enum here……"

……………>

Now define Window resource

<Window.Resources>

<ObjectDataProvider MethodName="GetValues"

ObjectType="{x:Type sys:Enum}"

x:Key="InsuranceExcess">

<ObjectDataProvider.MethodParameters>

<x:Type TypeName="n:Excess" />

ObjectDataProvider.MethodParameters>

ObjectDataProvider>

Window.Resources>

At last to bind the combo box with the enum

<ComboBox Name="cmbInsuranceExcess" DataContext="{StaticResource InsuranceExcess}" ItemsSource="{Binding}" >

I hope this will help to bind enum to combo box.

No comments: