ObjectDataProvider
不支持这种功能,但您可以通过巧妙地滥用aBinding
和 an来“伪造”它IValueConverter
。
首先,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();
}
}
以下是你如何使用它:
<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>
一些测试枚举:
enum MyEnum1
{
Red,
Green,
Blue,
}
enum MyEnum2
{
Cat,
Dog,
Fish,
Bird,
}
这会产生以下输出:

这利用了您可以将额外参数传递给 an 的事实,IValueConverter
我使用该参数Type
将枚举的 传递给转换器。转换器只是调用Enum.GetNames
该参数,并返回结果。实际Binding
情况实际上将绑定到发生DataContext
的任何ComboBox
情况。EnumConverter
只是很高兴地忽略它并改为对参数进行操作。
更新
通过直接绑定到类型,完全跳过它,它可以更好地工作ConverterParameter
,如下所示:
<ComboBox ItemsSource="{Binding Source={x:Type local:MyEnum1}, Converter={StaticResource EnumConverter}}" />
通过对转换器的调整:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.GetValues((Type)value);
}
结果相同,输入更少,代码更容易理解。