Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I have an enum bound combobox with custom string formatting for enum values?
    primarykey
    data
    text
    <p>In the post <a href="https://stackoverflow.com/questions/479410/enum-tostring">Enum ToString</a>, a method is described to use the custom attribute <code>DescriptionAttribute</code> like this:</p> <pre><code>Enum HowNice { [Description("Really Nice")] ReallyNice, [Description("Kinda Nice")] SortOfNice, [Description("Not Nice At All")] NotNice } </code></pre> <p>And then, you call a function <code>GetDescription</code>, using syntax like:</p> <pre><code>GetDescription&lt;HowNice&gt;(NotNice); // Returns "Not Nice At All" </code></pre> <p>But that doesn't really help me <strong>when I want to simply populate a ComboBox with the values of an enum, since I cannot force the ComboBox to call <code>GetDescription</code></strong>.</p> <p>What I want has the following requirements:</p> <ul> <li>Reading <code>(HowNice)myComboBox.selectedItem</code> will return the selected value as the enum value.</li> <li>The user should see the user-friendly display strings, and not just the name of the enumeration values. So instead of seeing "<code>NotNice</code>", the user would see "<code>Not Nice At All</code>".</li> <li>Hopefully, the solution will require minimal code changes to existing enumerations.</li> </ul> <p>Obviously, I could implement a new class for each enum that I create, and override its <code>ToString()</code>, but that's a lot of work for each enum, and I'd rather avoid that.</p> <p>Any ideas?</p> <p>Heck, I'll even throw in a <a href="https://stackoverflow.com/questions/795979/retrieve-a-list-of-the-most-popular-get-param-variations-for-a-given-url">hug</a> as a bounty :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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