WPF binding ComboBox to enum (with a twist)

Posted by Carlo on Stack Overflow See other posts from Stack Overflow or by Carlo
Published on 2009-05-27T15:04:12Z Indexed on 2010/04/12 16:23 UTC
Read the original article Hit count: 1179

Filed under:
|

Well the problem is that I have this enum, BUT I don't want the combobox to show the values of the enum. This is the enum:

public enum Mode
    {
    	[Description("Display active only")]
    	Active,
    	[Description("Display selected only")]
    	Selected,
    	[Description("Display active and selected")]
    	ActiveAndSelected
    }

So in the ComboBox instead of displaying Active, Selected or ActiveAndSelected, I want to display the DescriptionProperty for each value of the enum. I do have an extension method called GetDescription() for the enum:

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;
    		}
    	}

So is there a way I can bind the enum to the ComboBox AND show it's content with the GetDescription extension method?

Thanks!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding