將枚舉屬性數(shù)據(jù)庫為WPF中的組合框例如,以下代碼為例:public enum ExampleEnum { FooBar, BarFoo }public class ExampleClass : INotifyPropertyChanged{
private ExampleEnum example;
public ExampleEnum ExampleProperty
{ get { return example; } { /* set and notify */; } }}我希望將屬性ExampleProperty數(shù)據(jù)庫綁定到ComboBox,這樣它就可以顯示“fooBar”和“barfoo”選項,并在雙模式下工作。最理想的是,我希望我的ComboBox定義如下所示:<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />目前,我在我的窗口中安裝了ComboBox.electionChanged和ExampleClass.PropertyChanged事件的處理程序,我在窗口中手動進(jìn)行綁定。有什么更好的或者某種規(guī)范的方法嗎?您是否通常使用轉(zhuǎn)換器,以及如何使用正確的值填充ComboBox?我現(xiàn)在甚至不想開始使用i18n。編輯因此,有一個問題得到了回答:如何用正確的值填充ComboBox。從靜態(tài)Enum.GetValue方法中通過ObjectDataProvider將Enum值檢索為字符串列表:<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ExampleEnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ExampleEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider></Window.Resources>我可以將它用作ComboBox的ItemsSource:<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
將枚舉屬性數(shù)據(jù)庫為WPF中的組合框
12345678_0001
2019-07-26 15:14:54