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.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
EnumHelperLib
EnumHelperLib_Call
EnumHelperLib_Test
.gitignore
Doxyfile
EnumHelperLib.sln
LICENSE
README.md

README.md

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>;

[TypeConverter(typeof(EnumTypeConverter<NewLineCodes>))]
public enum NewLineCodes {
  [ExtraProperties("Entity:'\n', SecondKey:'Not Used'")]
  [Description("Unix(LF)")]
  Lf,
  [ExtraProperties("Entity : \"\r\"")]
  [Description("Mac(CR)")]
  Cr,
  [ExtraProperties("Entity:\t'\r\n'")]
  [Description("Windows(CR+LF)")]
  CrLf,
}

// 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)

Link