Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Heres my usage of binding enums to a list/combox</p> <pre><code>public enum EnumsAvailable { [Description("Its an A")] a, [Description("Its a B")] b, [Description("Its a C")] c, [Description("Its a D")] d } ; </code></pre> <p>Here is my XAML</p> <pre><code> &lt;ComboBox Grid.Column="4" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="cb_Application" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Path=ListOfEnumValues,Converter={converters:ArrayStringToEnumDescriptionConverter}}" SelectedValue="{Binding Path=ChosenEnum,Converter={converters:DescriptionToEnumConverter},UpdateSourceTrigger=PropertyChanged}" Validation.ErrorTemplate="{x:Null}" TabIndex="5" /&gt; </code></pre> <p>My View Model</p> <pre><code> public EnumsAvailable ListOfEnumValues { get { return new EnumsAvailable(); } } public EnumsAvailable ChosenEnum { get { return _ChosenEnum; } set { if (_ChosenEnum != value) { _ChosenEnum = value; RaisePropertyChanged(() =&gt; ChosenEnum); } } } </code></pre> <p>and my convertors</p> <pre><code>public class ArrayStringToEnumDescriptionConverter : BaseEnumDescriptionConverter, IValueConverter { public ArrayStringToEnumDescriptionConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var type = value.GetType(); return !type.IsEnum ? null : base.GetEnumDescription(type); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } public abstract class BaseEnumDescriptionConverter : MarkupExtension { public override object ProvideValue(IServiceProvider serviceProvider) { return this; } public IEnumerable&lt;string&gt; GetEnumDescription(Type destinationType) { var enumType = destinationType; var values = RetrieveEnumDescriptionValues(enumType); return new List&lt;string&gt;(values); } public object GetEnumFromDescription(string descToDecipher, Type destinationType) { var type = destinationType; if (!type.IsEnum) throw new InvalidOperationException(); var staticFields = type.GetFields().Where(fld =&gt; fld.IsStatic); foreach (var field in staticFields) { var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute != null) { if (attribute.Description == descToDecipher) { return (Enum.Parse(type, field.Name, true)); } } else { if (field.Name == descToDecipher) return field.GetValue(null); } } throw new ArgumentException("Description is not found in enum list."); } public static string[] RetrieveEnumDescriptionValues(Type typeOfEnum) { var values = Enum.GetValues(typeOfEnum); return (from object fieldInfo in values select DescriptionAttr(fieldInfo)).ToArray(); } public static string DescriptionAttr(object enumToQuery) { FieldInfo fi = enumToQuery.GetType().GetField(enumToQuery.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); return attributes.Length &gt; 0 ? attributes[0].Description : enumToQuery.ToString(); } public static string GetDomainNameAttribute(object enumToQuery) { FieldInfo fi = enumToQuery.GetType().GetField(enumToQuery.ToString()); DomainNameAttribute[] attributes = (DomainNameAttribute[])fi.GetCustomAttributes( typeof(DomainNameAttribute), false); return attributes.Length &gt; 0 ? attributes[0].DomainName : enumToQuery.ToString(); } } public class DescriptionToEnumConverter : BaseEnumDescriptionConverter, IValueConverter { public DescriptionToEnumConverter(){} public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var enumValue = value; if (enumValue != null) { enumValue = GetEnumFromDescription(value.ToString(), targetType); } return enumValue; } } </code></pre> <p>In my personal opinion its alot cleaner and highly re-usable. The only flaw I've found so far with it is if you update the selected value in code(not via UI) then its not updating on the UI. But this can be overcome with some further UI tweaking. I'm doing the change now and I'll update this answer once I've completed it.</p>
    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. This table or related slice is empty.
    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