如何将枚举值绑定到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为空字符串。谁能解释我所缺少的吗?
试试这个,
public static List<object> GetDataSource(Type type, bool fillEmptyField = false)
{
if (type.IsEnum)
{
var data = Enum.GetValues(type).Cast<Enum>()
.Select(E => new { Key = (object)Convert.ToInt16(E), Value = ToolsHelper.GetEnumDescription(E) })
.ToList<object>();
var emptyObject = new {Key = default(object), Value = ""};
if (fillEmptyField)
{
data.Insert(0, emptyObject); // insert the empty field into the combobox
}
return data;
}
return null;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句