将枚举属性数据库为WPF中的组合框例如,以下代码为例:public enum ExampleEnum { FooBar, BarFoo }public class ExampleClass : INotifyPropertyChanged{
private ExampleEnum example;
public ExampleEnum ExampleProperty
{ get { return example; } { /* set and notify */; } }}我希望将属性ExampleProperty数据库绑定到ComboBox,这样它就可以显示“fooBar”和“barfoo”选项,并在双模式下工作。最理想的是,我希望我的ComboBox定义如下所示:<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />目前,我在我的窗口中安装了ComboBox.electionChanged和ExampleClass.PropertyChanged事件的处理程序,我在窗口中手动进行绑定。有什么更好的或者某种规范的方法吗?您是否通常使用转换器,以及如何使用正确的值填充ComboBox?我现在甚至不想开始使用i18n。编辑因此,有一个问题得到了回答:如何用正确的值填充ComboBox。从静态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贡献1815条经验 获得超6个赞
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贡献1865条经验 获得超7个赞
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 关注
- 450 浏览
添加回答
举报
0/150
提交
取消
