ObjectDataProvider doesn't support that kind of functionality, but you can "fake" it with the clever abuse usage of a Binding and an IValueConverter.
First, the IValueConverter:
class EnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.GetValues((Type)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Here is how you use it:
<Window
x:Class="EnumTest.MainWindow"
[...snip...]
xmlns:local="clr-namespace:EnumTest"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.Resources>
<local:EnumConverter x:Key="EnumConverter" />
</Window.Resources>
<StackPanel>
<ComboBox ItemsSource="{Binding Converter={StaticResource EnumConverter}, ConverterParameter={x:Type local:MyEnum1}}" />
<ComboBox ItemsSource="{Binding Converter={StaticResource EnumConverter}, ConverterParameter={x:Type local:MyEnum2}}" />
</StackPanel>
</Window>
Some Test Enums:
enum MyEnum1
{
Red,
Green,
Blue,
}
enum MyEnum2
{
Cat,
Dog,
Fish,
Bird,
}
This produces the following output:

This takes advantage of the fact that you can pass an extra parameter to an IValueConverter, which I use to pass the Type of the enumeration to the converter. The converter just calls Enum.GetNames on that argument, and returns the result. The actual Binding will actually be bound to whatever the DataContext of the ComboBox happens to be. The EnumConverter just happily ignores it and operates on the parameter instead.
UPDATE
It works even better by binding directly to the type, skipping the ConverterParameter entirely, like so:
<ComboBox ItemsSource="{Binding Source={x:Type local:MyEnum1}, Converter={StaticResource EnumConverter}}" />
With adjustments to the converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.GetValues((Type)value);
}
Same result with less typing, and easier to understand, code.
ObjectDataProviderdoesn't support that kind functionality. You need a provider per type. The ODP just acts as a thin wrapper around the method call. - Bradley UffnerIValueConverterto get the kind of functionality you want though. Give me a few minutes to try it, and if it works out, I'll post an answer for you. - Bradley Uffner