作为一个例子采取下面的代码:数据绑定一个枚举属性的组合框在WPF
public enum ExampleEnum { FooBar, BarFoo }
public class ExampleClass : INotifyPropertyChanged
{
private ExampleEnum example;
public ExampleEnum ExampleProperty
{ get { return example; } { /* set and notify */; } }
}
我想向物业ExampleProperty数据绑定到一个组合框,以便它显示选项“FooBar的”和“ BarFoo“并以TwoWay模式工作。最理想的我希望我的组合框定义是这个样子:
<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />
目前我对ComboBox.SelectionChanged处理器和ExampleClass.PropertyChanged安装在我的窗口,我做的手工绑定事件。
有没有更好的或某种规范的方式?你通常会使用转换器,你将如何使用正确的值填充组合框?我甚至不想立即开始使用i18n。
编辑
所以一个问题得到回答:如何填充正确的价值观的组合框。
通过从静态Enum.GetValues方法的ObjectDataProvider的检索枚举值作为字符串列表:
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ExampleEnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ExampleEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
这我可以作为一个的ItemsSource用我的组合框:
<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
我探讨了这一点,并有一个解决方案,您可以使用(完成本地化)在WPF位于[这里](http://www.ageektrapped.com/blog/the-missing-net-7-displaying-enums-在-WPF /)。 – ageektrapped 2008-09-19 00:52:56