[英]How to bind an enumeration to combobox
我会使用组合框控件绑定枚举的值。
我写了这段代码:
cboPriorLogicalOperator.DataSource = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(p => new { Key = (int)p, Value = p.ToString() })
.ToList();
myComboBox.DisplayMember = "Value";
myComboBox.ValueMember = "Key";
它运作良好,但我想知道是否有一个更简单的方法。
我觉得你的代码很漂亮!
唯一的改进是将代码放在扩展方法中 。
编辑:
当我仔细想想,你想做的事就是使用Enum
作为定义,而不是枚举,这是扩展方法所要求的一个实例。
public class SelectList
{
// Normal SelectList properties/methods go here
public static SelectList Of<T>()
{
Type t = typeof(T);
if (t.IsEnum)
{
var values = from Enum e in Enum.GetValues(t)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
return null;
}
}
// called with
var list = SelectList.Of<Things>();
只有你可能想要返回一个Dictionary<int, string>
而不是一个SelectList
,但你明白了。
EDIT2:
在这里,我们将使用代码示例来介绍您正在查看的案例。
public class EnumList
{
public static IEnumerable<KeyValuePair<T, string>> Of<T>()
{
return Enum.GetValues(typeof (T))
.Cast<T>()
.Select(p => new KeyValuePair<T, string>(p, p.ToString()))
.ToList();
}
}
或许这个版本,其中键是一个int
public class EnumList
{
public static IEnumerable<KeyValuePair<int, string>> Of<T>()
{
return Enum.GetValues(typeof (T))
.Cast<T>()
.Select(p => new KeyValuePair<int, string>(Convert.ToInt32(p), p.ToString()))
.ToList();
}
}
为什么不使用:
myComboBox.DataSource = Enum.GetValues(typeof(MyEnum))
?
foreach (int r in Enum.GetValues(typeof(MyEnum)))
{
var item = new ListItem(Enum.GetName(typeof(MyEnum), r), r.ToString());
ddl.Items.Add(item);
}
我最近遇到了一个问题,我有一个可以为空的枚举属性,需要将它绑定到ComboBox。 这是我提出的解决方案:
using System;
using System.Collections.Generic;
namespace ActivitySchedule.Model
{
public class NullableEnum<T> where T : struct, IComparable
{
public string Display { get; private set; }
public T? Value { get; private set; }
public static implicit operator T?(NullableEnum<T> o)
{
return o.Value;
}
public static implicit operator NullableEnum<T>(T? o)
{
return new NullableEnum<T>
{
Display = o?.ToString() ?? "NA",
Value = o
};
}
private NullableEnum() { }
public static IEnumerable<NullableEnum<T>> GetList()
{
var items = new List<NullableEnum<T>>
{
new NullableEnum<T>
{
Display = "NA",
Value = null
}
};
var values = Enum.GetValues(typeof(T));
foreach (T v in values)
{
items.Add(v);
}
return items;
}
}
}
我将对象包装在Controller类中并更改属性的类型,如下所示:
private MyClass myClass;
public NullableEnum<MyEnum> MyEnum
{
get { return this.myClass.MyEnum; }
set { this.myClass.MyEnum = value.Value; }
}
(它也可以是派生类并覆盖属性)
这就是我使用它的方式:
var types = NullableEnum<MyEnum>.GetList();
this.comboBox1.DataSource = types;
this.comboBox1.DisplayMember = "Display";
this.comboBox1.ValueMember = "Value";
this.comboBox1.Bindings.Add("SelectedValue", myClassController, "MyEnum");
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = Enum.GetValues( typeof(Gender));
Array gen = Enum.GetValues(typeof(Gender));
List<KeyValuePair<string, char>> lstgender = new List<KeyValuePair<string,char>>();
foreach(Gender g in gen)
lstgender.Add(new KeyValuePair<string,char>(g.ToString(),((char)g)));
comboBox1.DataSource = lstgender;
comboBox1.DisplayMember = "key";
comboBox1.ValueMember = "value"
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
public class Student
{
public string stud_name { get; set; }
public Gender stud_gen { get; set; }
}
public enum Gender
{
Male='M',
Female='F'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.