Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thank you all for all answers. Finally I used a combination from Rex M and adrianbanks, and added my own improvements, to simplify the binding to ComboBox.</p> <p>The changes were needed because, while working on the code, I realized sometimes I need to be able to exclude one enumeration item from the combo. E.g. </p> <pre><code>Enum Complexity { // this will be used in filters, // but not in module where I have to assign Complexity to a field AllComplexities, NotSoComplex, LittleComplex, Complex, VeryComplex } </code></pre> <p>So sometimes I want the picklist to show all but AllComplexities (in add - edit modules) and other time to show all (in filters)</p> <p>Here's what I did:</p> <ol> <li>I created a extension method, that uses Description Attribute as localization lookup key. If Description attribute is missing, I create the lookup localization key as EnumName_ EnumValue. Finally, if translation is missing I just split enum name based on camelcase to separate words as shown by adrianbanks. BTW, TranslationHelper is a wrapper around resourceMgr.GetString(...)</li> </ol> <p>The full code is shown below</p> <pre><code>public static string GetDescription(this System.Enum value) { string enumID = string.Empty; string enumDesc = string.Empty; try { // try to lookup Description attribute FieldInfo field = value.GetType().GetField(value.ToString()); object[] attribs = field.GetCustomAttributes(typeof(DescriptionAttribute), true); if (attribs.Length &gt; 0) { enumID = ((DescriptionAttribute)attribs[0]).Description; enumDesc = TranslationHelper.GetTranslation(enumID); } if (string.IsNullOrEmpty(enumID) || TranslationHelper.IsTranslationMissing(enumDesc)) { // try to lookup translation from EnumName_EnumValue string[] enumName = value.GetType().ToString().Split('.'); enumID = string.Format("{0}_{1}", enumName[enumName.Length - 1], value.ToString()); enumDesc = TranslationHelper.GetTranslation(enumID); if (TranslationHelper.IsTranslationMissing(enumDesc)) enumDesc = string.Empty; } // try to format CamelCase to proper names if (string.IsNullOrEmpty(enumDesc)) { Regex capitalLetterMatch = new Regex("\\B[A-Z]", RegexOptions.Compiled); enumDesc = capitalLetterMatch.Replace(value.ToString(), " $&amp;"); } } catch (Exception) { // if any error, fallback to string value enumDesc = value.ToString(); } return enumDesc; } </code></pre> <p>I created a generic helper class based on Enum, which allow to bind the enum easily to DataSource</p> <pre><code>public class LocalizableEnum { /// &lt;summary&gt; /// Column names exposed by LocalizableEnum /// &lt;/summary&gt; public class ColumnNames { public const string ID = "EnumValue"; public const string EntityValue = "EnumDescription"; } } public class LocalizableEnum&lt;T&gt; { private T m_ItemVal; private string m_ItemDesc; public LocalizableEnum(T id) { System.Enum idEnum = id as System.Enum; if (idEnum == null) throw new ArgumentException(string.Format("Type {0} is not enum", id.ToString())); else { m_ItemVal = id; m_ItemDesc = idEnum.GetDescription(); } } public override string ToString() { return m_ItemDesc; } public T EnumValue { get { return m_ID; } } public string EnumDescription { get { return ToString(); } } } </code></pre> <p>Then I created a generic static method that returns a List>, as below</p> <pre><code>public static List&lt;LocalizableEnum&lt;T&gt;&gt; GetEnumList&lt;T&gt;(object excludeMember) { List&lt;LocalizableEnum&lt;T&gt;&gt; list =null; Array listVal = System.Enum.GetValues(typeof(T)); if (listVal.Length&gt;0) { string excludedValStr = string.Empty; if (excludeMember != null) excludedValStr = ((T)excludeMember).ToString(); list = new List&lt;LocalizableEnum&lt;T&gt;&gt;(); for (int i = 0; i &lt; listVal.Length; i++) { T currentVal = (T)listVal.GetValue(i); if (excludedValStr != currentVal.ToString()) { System.Enum enumVal = currentVal as System.Enum; LocalizableEnum&lt;T&gt; enumMember = new LocalizableEnum&lt;T&gt;(currentVal); list.Add(enumMember); } } } return list; } </code></pre> <p>and a wrapper to return list with all members</p> <pre><code>public static List&lt;LocalizableEnum&lt;T&gt;&gt; GetEnumList&lt;T&gt;() { return GetEnumList&lt;T&gt;(null); } </code></pre> <p>Now let's put all things together and bind to actual combo:</p> <pre><code>// in module where we want to show items with all complexities // or just filter on one complexity comboComplexity.DisplayMember = LocalizableEnum.ColumnNames.EnumValue; comboComplexity.ValueMember = LocalizableEnum.ColumnNames.EnumDescription; comboComplexity.DataSource = EnumHelper.GetEnumList&lt;Complexity&gt;(); comboComplexity.SelectedValue = Complexity.AllComplexities; // .... // and here in edit module where we don't want to see "All Complexities" comboComplexity.DisplayMember = LocalizableEnum.ColumnNames.EnumValue; comboComplexity.ValueMember = LocalizableEnum.ColumnNames.EnumDescription; comboComplexity.DataSource = EnumHelper.GetEnumList&lt;Complexity&gt;(Complexity.AllComplexities); comboComplexity.SelectedValue = Complexity.VeryComplex; // set default value </code></pre> <p>To read selected the value and use it, I use code as below</p> <pre><code>Complexity selComplexity = (Complexity)comboComplexity.SelectedValue; </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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