Click here to Skip to main content
13,863,516 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 Apr 2009
Licenced CC (Attr 3U)

Binding to Enumerations

, 14 Apr 2010
Rate this:
Please Sign up or sign in to vote.
How to bind to enumerations

Every now and then, you find yourself needing to bind some control or the other to the values of an enumeration, such as System.DayOfWeek. You'd think this would be fairly simple, but it's actually trickier than you think.

I am fortunate to work with Paul Jackson, who's a very clever chap, especially when it comes to WPF. Paul blogged about the ins and outs of binding to enumerations some time ago and I keep finding myself having to search for it, so I thought I'd add it to my own blog for safe keeping.

To summarise, you need to do the following:

  1. Add the appropriate XML namespace.
  2. Add an ObjectDataProvider with the MethodName set to GetValues.
  3. Set the ItemsSource property of your control.
<Window xmlns=""

             xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>
        <ObjectDataProvider x:Key="DayValues"


                            ObjectType="{x:Type sys:Enum}">
                <x:Type TypeName="sys:DayOfWeek" />
        <ComboBox x:Name="_days"

                  ItemsSource="{Binding Source={StaticResource DayValues}}" />

What Paul's article doesn't mention is how to specify the SelectedValue. The following code example shows how to set the SelectedValue for our ComboBox above to be DayOfWeek.Monday.

<ComboBox x:Name="_days"

          ItemsSource="{Binding Source={StaticResource DayValues}}"

          SelectedValue="{x:Static sys:DayOfWeek.Monday}" />

Thanks Paul :).


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution 3.0 Unported License


About the Author

Derek Lakin
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

Generalok article Pin
Donsw12-May-09 7:41
memberDonsw12-May-09 7:41 
GeneralRe: ok article Pin
Derek Lakin12-May-09 7:46
memberDerek Lakin12-May-09 7:46 
GeneralSimple Method here... Pin
stixoffire6-Apr-09 20:37
memberstixoffire6-Apr-09 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 15 Apr 2010
Article Copyright 2009 by Derek Lakin
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid