WPF - using an ObjectDataProvider

by Gregor Uhlenheuer on February 6, 2012

The ObjectDataProvider is a pretty neat construct in WPF to build custom data providers for existing types. I find those especially useful when working with enumerations. If you want to list all possible values of an enumeration in a ComboBox, this are the steps you have to take.

First you have to create a static instance of the ObjectDataProvider in some resource dictionary. In the resources of the current window this could look like the following:

The above lines are roughly equivalent to the invokation of GetValues in your code-behind:

The next step is to add a static binding for the ItemsSource of your container to the new data provider:

Notice that you may have to add the necessary namespace declarations (for System in mscorlib and your project’s own namespace) in your XAML’s header like:


Discriminated unions in F#

Using a discriminated union in F# sadly does not work the same way. In order to obtain the types of a union type you have to use reflection and bind the values manually to the container’s ItemsSource.

In case you really want to use a real enumeration in F# you have to define the discriminated union like this instead:

This post is tagged with programming, .net and wpf