Binding an enumeration to a ComboBox can be done in several ways. In most cases you don't want to display the value itself, but a more user friendly description. One common approach is to use the DescriptionAttribute on the Enum values to supply a description for each value. This is all possible in a very MVVM friendly way.
First step is to add the DescriptionAttribute to the values of the enumeration.
To retrieve the description from the enum we use a simple extension method. This method returns the value of the DescriptionAttribute if it exists, otherwise the string representation of the enum value is returned.
With this extension method it is really easy to write a generic enum description converter to use where ever an enum value is presented to the user. But the more interesting case is where we want to present the enumeration in a ComboBox or a similar control. To accomplish this you can use a MarkupExtension. (Please note that this code is stripped from all type validation and such to be easier to read.)
The DescriptionAttribute together with these extensions gives us a really neat way of populating the values of an enum into a ComboBox.
First step is to add the DescriptionAttribute to the values of the enumeration.
public enum MyValues { [Description("First value")] First, [Description("Second value")] Second }
To retrieve the description from the enum we use a simple extension method. This method returns the value of the DescriptionAttribute if it exists, otherwise the string representation of the enum value is returned.
public static string GetDescription(this Enum value) { var fieldInfo = value.GetType().GetField(value.ToString()); var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; return attribute != null ? attribute.Description : value.ToString(); }
With this extension method it is really easy to write a generic enum description converter to use where ever an enum value is presented to the user. But the more interesting case is where we want to present the enumeration in a ComboBox or a similar control. To accomplish this you can use a MarkupExtension. (Please note that this code is stripped from all type validation and such to be easier to read.)
public class EnumExtension : MarkupExtension { private readonly Type _enumType; public EnumExtension(Type enumType) { _enumType = enumType; } public override object ProvideValue(IServiceProvider serviceProvider) { return (from object enumValue in Enum.GetValues(_enumType) select new EnumMember {Value = enumValue, Description = ((Enum)enumValue).GetDescription()}).ToArray(); } public class EnumMember { public string Description { get; set; } public object Value { get; set; } } }
The DescriptionAttribute together with these extensions gives us a really neat way of populating the values of an enum into a ComboBox.
<ComboBox SelectedValue="{Binding MyValue}" ItemsSource="{Binding Source={my:EnumExtension {x:Type MyValues}}}" DisplayMemberPath="Description" SelectedValuePath="Value" />
1xbet 1xbet 10cric 10cric 카지노 카지노 온라인카지노 온라인카지노 1xbet 1xbet fun88 fun88 687
ReplyDeleteTania's Pro Titanium Hockey VR - The Tania's Pro - Titanium
ReplyDeleteTania's Pro is titanium rod in leg an upcoming titanium hoop earrings video game developed by titanium tent stakes Tania titanium chain for HTC Vive, the HTC Vive, PC, micro touch titanium trimmer Vive, PlayStation VR and PlayStation VR.