將枚舉屬性數(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事件的處理程序,我在窗口中手動進行綁定。有什么更好的或者某種規(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}}"/>
3 回答

素胚勾勒不出你
TA貢獻1827條經(jīng)驗 獲得超9個贊
enum Status{ [Description("Available.")] Available, [Description("Not here right now.")] Away, [Description("I don't have time right now.")] Busy}
xmlns:my="clr-namespace:namespace_to_enumeration_extension_class
<ComboBox ItemsSource="{Binding Source={my:Enumeration {x:Type my:Status}}}" DisplayMemberPath="Description" SelectedValue="{Binding CurrentStatus}" SelectedValuePath="Value" />
public class EnumerationExtension : MarkupExtension { private Type _enumType; public EnumerationExtension(Type enumType) { if (enumType == null) throw new ArgumentNullException("enumType"); EnumType = enumType; } public Type EnumType { get { return _enumType; } private set { if (_enumType == value) return; var enumType = Nullable.GetUnderlyingType(value) ?? value; if (enumType.IsEnum == false) throw new ArgumentException("Type must be an Enum."); _enumType = value; } } public override object ProvideValue(IServiceProvider serviceProvider) { var enumValues = Enum.GetValues(EnumType); return ( from object enumValue in enumValues select new EnumerationMember{ Value = enumValue, Description = GetDescription(enumValue) }).ToArray(); } private string GetDescription(object enumValue) { var descriptionAttribute = EnumType .GetField(enumValue.ToString()) .GetCustomAttributes(typeof (DescriptionAttribute), false) .FirstOrDefault() as DescriptionAttribute; return descriptionAttribute != null ? descriptionAttribute.Description : enumValue.ToString(); } public class EnumerationMember { public string Description { get; set; } public object Value { get; set; } } }

守著星空守著你
TA貢獻1799條經(jīng)驗 獲得超8個贊
public MyEnumType SelectedMyEnumType { get { return _selectedMyEnumType; } set { _selectedMyEnumType = value; OnPropertyChanged("SelectedMyEnumType"); } } public IEnumerable<MyEnumType> MyEnumTypeValues { get { return Enum.GetValues(typeof(MyEnumType)) .Cast<MyEnumType>(); } }
<ComboBox SelectedItem="{Binding SelectedMyEnumType}" ItemsSource="{Binding MyEnumTypeValues}"></ComboBox>
- 3 回答
- 0 關(guān)注
- 395 瀏覽
添加回答
舉報
0/150
提交
取消