Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add space to bound enum values?
    primarykey
    data
    text
    <p>In the post <a href="https://stackoverflow.com/questions/796607/how-do-i-override-tostring-in-c-sharp-enums">How do I have an enum bound combobox with custom string formatting for enum values?</a> and <a href="https://stackoverflow.com/questions/1101872/how-to-set-space-on-enum">How to set space on Enum</a> 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. </p> <p>My Enum goes something like below</p> <pre><code>[TypeConverter(typeof(EnumToStringUsingDescription))] public enum SCSRequestType { [Description ("Change Request")] ChangeRequest = 4, [Description("Documentation")] Documentation = 9,.... } </code></pre> <p>And also I am using the below typeconverter.</p> <pre><code>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 &gt; 0) ? ((DescriptionAttribute)attrs[0]).Description : name; } } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload