Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The way I have always done it is as follows. The beauty of this solution is that it is completely generic, and can be re-used for any enumeration types.</p> <p>1) When you define an enumeration make use of some custom attributes to give a little information. In this example I've used Browsable(false) to indicate that this enumerator is internal, and I don't want to see this option in a combo box. Description("") allows me to specify a Display name for the enumeration.</p> <pre><code>public enum MyEnumerationTypeEnum { [Browsable(false)] Undefined, [Description("Item 1")] Item1, [Description("Item 2")] Item2, Item3 } </code></pre> <p>2) Define a class which I've called EnumerationManager. This is a generic class that analyzes an Enumeration type and generates a list of values. If an enumerator has Browsable set to false it will be skipped. If it has a Description attribute then it will use the description string as the display name. If no description is found it will just show the default string of the enumerator.</p> <pre><code>public class EnumerationManager { public static Array GetValues(Type enumeration) { Array wArray = Enum.GetValues(enumeration); ArrayList wFinalArray = new ArrayList(); foreach(Enum wValue in wArray) { FieldInfo fi = enumeration.GetField(wValue.ToString()); if(null != fi) { BrowsableAttribute[] wBrowsableAttributes = fi.GetCustomAttributes(typeof(BrowsableAttribute),true) as BrowsableAttribute[]; if(wBrowsableAttributes.Length &gt; 0) { // If the Browsable attribute is false if(wBrowsableAttributes[0].Browsable == false) { // Do not add the enumeration to the list. continue; } } DescriptionAttribute[] wDescriptions = fi.GetCustomAttributes(typeof(DescriptionAttribute),true) as DescriptionAttribute[]; if(wDescriptions.Length &gt; 0) { wFinalArray.Add(wDescriptions[0].Description); } else wFinalArray.Add(wValue); } } return wFinalArray.ToArray(); } } </code></pre> <p>3) In your xaml add a section in your ResourceDictionary </p> <pre><code> &lt;ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type l:EnumerationManager}" x:Key="OutputListForMyComboBox"&gt; &lt;ObjectDataProvider.MethodParameters&gt; &lt;x:Type TypeName="l:MyEnumerationTypeEnum" /&gt; &lt;/ObjectDataProvider.MethodParameters&gt; &lt;/ObjectDataProvider&gt; </code></pre> <p>4) Now just bind the ItemsSource of your combobox to this key we just defined in the resource dictionary</p> <pre><code>&lt;ComboBox Name="comboBox2" ItemsSource="{Binding Source={StaticResource OutputListForMyComboBox}}" /&gt; </code></pre> <p>If you try this code using my enumeration above you should see 3 items in your combo box:</p> <pre><code>Item 1 Item 2 Item3 </code></pre> <p>Hope this helps.</p> <p>EDITS: If you add the implementation of the LocalizableDescriptionAttribute and use that instead the Description attribute that I was using that would be perfect.</p>
 

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