问题在于我有一个枚举类型,但我不想让下拉框显示枚举值。这是该枚举类型:
public enum Mode
{
[Description("Display active only")]
Active,
[Description("Display selected only")]
Selected,
[Description("Display active and selected")]
ActiveAndSelected
}
所以在ComboBox中,我想要显示枚举每个值对应的DescriptionProperty,而不是显示Active、Selected或ActiveAndSelected。对于枚举,我已经有一个名为GetDescription()的扩展方法:
public static string GetDescription(this Enum enumObj)
{
FieldInfo fieldInfo =
enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return enumObj.ToString();
}
else
{
DescriptionAttribute attrib =
attribArray[0] as DescriptionAttribute;
return attrib.Description;
}
}
那么我可以将枚举绑定到ComboBox并使用 GetDescription 扩展方法显示其内容吗?
谢谢!