Talk to Webmaster Tony John
|
Resources » .NET programming » WPF
Binding Enum With Combobox in wpf
It is very easy and efficient way to bind combo box in wpf some people will use reflection with Description Attribute which is again performance issue that is as per size of our enum it will take more space in memory.
This will bind Enums values with combobox in different ways
Simple Enum Binding
Binding With Helper Class
Binding With Dicitonary
|
It is very easy and efficient way to bind combo box in wpf some people will use reflection with Description Attribute which is again performance issue that is as per size of our enum it will take more space in memory. And this is good practice to ignore use of reflection if we can do the things without reflection.
There are several way to implement this :
Simple Way to Bind Enum With Combobox Object Data Provider we will put in Windows resources here we have to add <> > tags while using in code or application.
//XMAL File code //StaticResource Object Data Provider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="rv" Object Data Provider.MethodParameters
x:Type TypeName="xx:TheWeek" Object Data Provider.MethodParameters Object Data Provider //StaticResource
ComboBox ItemsSource="{Binding Source={StaticResource rv}}" IsSynchronizedWithCurrentItem="true"
//Code Behind File
public enum TheWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
Binding Enum's Description With Combobox
IF there is space between the enum's value like Sunday [Day of Sun] Then we Can use this approach also to show enum description
//Xmal Code //Resources ObjectDataProvider MethodName="GetDescription" ObjectType="{x:Type sys:Enum}" x:Key="rg" ObjectDataProvider.MethodParameters ObjectDataProvider.MethodParameters ObjectDataProvider
StackPanel ComboBox ItemsSource="{Binding Source={xx:Enumeration {x:Type xx:TheWeek}}}" DisplayMemberPath="Description" SelectedValue="{Binding CurrentStatus}" SelectedValuePath="Value" StackPanel
//Code Behind Helper Class which we need to show description attribute of enum public class EnumerationExtension : MarkupExtension { private Type _enumType;
public EnumerationExtension(Type enumType) { if (enumType == null) throw new ArgumentNullException("enumType");
EnumType = enumType; }
public Type EnumType { get { return _enumType; } private set { if (_enumType == value) return;
var enumType = Nullable.GetUnderlyingType(value) ?? value;
if (enumType.IsEnum == false) throw new ArgumentException("Type must be an Enum.");
_enumType = value; } }
public override object ProvideValue(IServiceProvider serviceProvider) { var enumValues = Enum.GetValues(EnumType);
return ( from object enumValue in enumValues select new EnumerationMember { Value = enumValue, Description = GetDescription(enumValue) }).ToArray(); }
private string GetDescription(object enumValue) { var descriptionAttribute = EnumType .GetField(enumValue.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false) .FirstOrDefault() as DescriptionAttribute;
return descriptionAttribute != null ? descriptionAttribute.Description : enumValue.ToString(); }
public class EnumerationMember { public string Description { get; set; } public object Value { get; set; } } }
Binding Enum's Description Using Dictionary With Combobox in WPF
// By taking list or dictionary we can bind enum values with static resources
//Xaml Code ComboBox ItemsSource="{Binding Source={StaticResource theWeekList }, Path= TheWeekList }"SelectedIndex="0" DisplayMemberPath="Value" SelectedValuePath="Key" public enum TheWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
_theWeekList = new Dictionary() { {TheWeek.Friday, "First Day"}, {TheWeek.Monday, "Second Day"}, {TheWeek.Saturday, "Third Day"}, {TheWeek.Sunday, "Fourth Day"}, {TheWeek.Thursday, "Fifth Day"}, {TheWeek.Tuesday, "Six Day"}, {TheWeek.Wednesday, "Seven Day"}, };
private static Dictionary _theWeekList; public static Dictionary TheWeekList { get { return _theWeekList; } }
Note : There are many way to do this task in WPF and this is just one of many ways you can achieve the same.
|
Did you like this resource? Share it with your friends and show your love!
|
|
|
No responses found. Be the first to respond...
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
Active MembersTodayLast 7 Daysmore...
|