0

Funky WPF – Enumerations and the Combo Box

Binding a combo box to an enumeration in WPF is more work than it should be, creating an object data provider etc etc:

<Window.Resources>
    <ObjectDataProvider MethodName="GetValues"
        ObjectType="{x:Type sys:Enum}"
        x:Key="CharacterEnumValues">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="Character" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

Followed by

<ComboBox SelectedItem="{Binding Character}"
ItemsSource="{Binding
Source={StaticResource CharacterValues}} "/>

What a pain! I have just added ‘EnumerationComboBox’ to my Apex library – so now you can do this:

<!-- The combo box, bound to an enumeration. -->
<apexControls:EnumerationComboBox 
SelectedEnumeration="{Binding Character}" />

No need for an ObjectDataProvider, an items source or anything – and if you decorate enum’s with the ‘[Description]’ attribute, it’ll use the description in the combo.

There’s an article/download here for anyone who’s interested:

http://www.codeproject.com/KB/WPF/enumcombobox.aspx