【发布时间】:2010-10-29 07:30:26
【问题描述】:
问题是我有这个枚举,但我不希望组合框显示枚举的值。这是枚举:
public enum Mode
{
[Description("Display active only")]
Active,
[Description("Display selected only")]
Selected,
[Description("Display active and selected")]
ActiveAndSelected
}
所以在 ComboBox 中,我不想显示 Active、Selected 或 ActiveAndSelected,而是显示枚举的每个值的 DescriptionProperty。我确实为枚举提供了一个名为 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 扩展方法显示它的内容?
谢谢!
【问题讨论】:
标签: wpf data-binding enums combobox