Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Binding to Enumerations

, 14 Apr 2010 CC (Attr 3U)
Rate this:
Please Sign up or sign in to vote.
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.

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:sys="clr-namespace:System;assembly=mscorlib" ...>
    <Window.Resources>
        <ObjectDataProvider x:Key="DayValues"
                            MethodName="GetValues"
                            ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="sys:DayOfWeek" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        ...
        <ComboBox x:Name="_days"
                  ItemsSource="{Binding Source={StaticResource DayValues}}" />
        ...
    </Grid>
</Window>

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 Smile | :) .

License

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

Share

About the Author

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

Comments and Discussions

 
Generalok article PinmemberDonsw12-May-09 7:41 
GeneralRe: ok article PinmemberDerek Lakin12-May-09 7:46 
GeneralSimple Method here... Pinmemberstixoffire6-Apr-09 20:37 

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

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

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