0

In the post How do I have an enum bound combobox with custom string formatting for enum values? and How to set space on Enum shows how to add descriptions to Enum values to be able to add spaces which is usable when binding comboboxes to string values. My case is that the selected item of this combobox is also bound to another control. using the solutions i have found with that link, the value of the selected item that I am getting is still the value of the Enum without the spaces.

My Enum goes something like below

[TypeConverter(typeof(EnumToStringUsingDescription))]
public enum SCSRequestType {       
   [Description ("Change Request")]
   ChangeRequest = 4,

   [Description("Documentation")]
   Documentation = 9,....
}

And also I am using the below typeconverter.

public class EnumToStringUsingDescription : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return (sourceType.Equals(typeof(Enum)));
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return (destinationType.Equals(typeof(String)));
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (!destinationType.Equals(typeof(String)))
        {
            throw new ArgumentException("Can only convert to string.", "destinationType");
        }

        if (!value.GetType().BaseType.Equals(typeof(Enum)))
        {
            throw new ArgumentException("Can only convert an instance of enum.", "value");
        }

        string name = value.ToString();
        object[] attrs =
            value.GetType().GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
        return (attrs.Length > 0) ? ((DescriptionAttribute)attrs[0]).Description : name;
    }
}

Am I missing something here, how can i force the combobox's selected item to be the value of the descriptions of the Enum values. I am also open for alternatives.

Community
  • 1
  • 1
Jepoy_D_Learner
  • 435
  • 1
  • 6
  • 13

2 Answers2

0

The best way I've found is in this post. It creates a markup extension, and then you simply bind to your combo box.

Community
  • 1
  • 1
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36
0

Or you could set up an extension. Create public static class Extensions in the toplevel of your namespace. In there you create this method:

public static string getDescription(this SCSRequestType scs)
{
    switch (scs)
    {
        case SCSRequestType.ChangeRequest:
            return "Change Request";
        case SCSRequestType.Documentation:
            return "Documentation";
        default:
            return null;
    }
}

Finally you call string description = SCSRequestType.ChangeRequest.getDescription(); to access the description.

Jan
  • 2,168
  • 2
  • 19
  • 28
  • I am not sure if this is the right track, my other control is bounded to the selected item of combobox in xaml. Using this solution is like having switch-case statement inside a property bound to the selected item like below 'public string SCSRequestTypeSelectedItem(){get; set{ if(_SCSRequestType != value) // I can use a switch-case here to add spaces }} '. For me this is messy and repeatitive. – Jepoy_D_Learner Aug 08 '12 at 22:45