【发布时间】:2013-12-14 08:17:12
【问题描述】:
如何将枚举值绑定到 ComboBox 并使用 Linq 用空字段填充它?我试过了:
public static List<object> GetDataSource(Type type, bool fillEmptyField = false)
{
if (type.IsEnum)
{
if (fillEmptyField)
{
var data = Enum.GetValues(type)
.Cast<Enum>()
.Select(E => new { Key = (object)Convert.ToInt16(E), Value = ToolsHelper.GetEnumDescription(E) })
.ToList<object>();
return data;
}
else
{
return Enum.GetValues(type)
.Cast<Enum>()
.Select(E => new { Key = Convert.ToInt16(E), Value = ToolsHelper.GetEnumDescription(E) })
.ToList<object>();
}
}
return null;
}
但我不知道如何将空字段插入组合框中,但是 Key 为 null 而 Value 为空字符串。谁能解释我错过了什么?
【问题讨论】: