Helps binding an enum with description to ComboBox. Attach Dictionary to enum item.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

EnumHelper<TEnum> Class

  • It helps binding an enum with description to ComboBox.
  • Enum item description come from:
    1. localized resource (EnumType_ItemName)
    2. default resource (EnumType_ItemName)
    3. Description attribute (System.ComponentModel)
    4. Enum item name
  • Not only ValueDescriptionPairs but also EnumTypeConverter<TEnum> can be used when the UIElement support TypeConverter.
  • ExtraProperties attribute attaches a Dictionary<string, string>.
  • GetExtraProperty extension method returns the value from the dictionary.

Usage Sample

using System.ComponentModel;
using TakeAsh;

using NewLineCodesHelper = EnumHelper<NewLineCodes>;

public enum NewLineCodes {
  [ExtraProperties("Entity:'\n', SecondKey:'Not Used'")]
  [ExtraProperties("Entity : \"\r\"")]

// Resource
string NewLineCodes_Lf = "Unix(LF) [Localized]";
string NewLineCodes_Cr = "Mac(CR) [Localized]";
string NewLineCodes_CrLf = "Windows(CR+LF) [Localized]";

// using ValueDescriptionPairs
comboBox_NewLineCode_GalleryCategory.ItemsSource = NewLineCodesHelper.ValueDescriptionPairs;
comboBox_NewLineCode_GalleryCategory.DisplayMemberPath = "Value";
comboBox_NewLineCode_Gallery.SelectedValuePath = "Key";
comboBox_NewLineCode_Gallery.SelectedValue = NewLineCodes.CrLf;

var newLineCode = NewLineCodesHelper.Cast(comboBox_NewLineCode_Gallery.SelectedValue);
var desc1 = newLineCode.ToDescription();
var desc2 = NewLineCodesHelper.ValueDescriptionDictionary[newLineCode];
var newLineEntity = newLineCode.GetExtraProperty("Entity");

var newLineEntities = NewLineCodesHelper.GetAllExtraProperties("Entity");

// using TypeConverter
comboBox_NewLineCode2_GalleryCategory.ItemsSource = NewLineCodesHelper.Values;
comboBox_NewLineCode2_Gallery.SelectedItem = NewLineCodes.CrLf;

var newLineCode2 = NewLineCodesHelper.Cast(comboBox_NewLineCode2_Gallery.SelectedItem);

Properties and Methods

EnumHelper<TEnum> Class Properties

  • string[] Descriptions [get]
  • string[] Names [get]
  • Dictionary<TEnum, string> ValueDescriptionDictionary [get]
  • KeyValuePair<TEnum, string>[] ValueDescriptionPairs [get]
  • TEnum[] Values [get]

EnumHelper<TEnum> Class Methods

  • TEnum Cast(object obj)
  • string[] GetAllExtraProperties(string key)
  • string GetAssemblyName()
  • TEnum GetValueFromDescription(string description)
  • TEnum GetValueFromName(string name)
  • Nullable<TEnum> GetValueNullableFromDescription(string description)
  • Nullable<TEnum> GetValueNullableFromName(string name)
  • void Init()
  • bool IsDefined(object value)
  • string ToDescription(TEnum en)
  • bool TryParse(string value, out TEnum result)

EnumExtensionMethods Class Methods

  • string GetAssemblyName(this Enum en)
  • string GetExtraProperty(this Enum en, string key)
  • string ToDescription(this Enum en)