You can bind a ComboBox with Enum description in C# using the following steps:
Description
attribute:public enum Gender { [Description("Male")] Male = 0, [Description("Female")] Female = 1, [Description("Other")] Other = 2 }
public static List<KeyValuePair<int, string>> GetEnumValues<T>() where T : struct, IConvertible { var values = Enum.GetValues(typeof(T)).Cast<T>(); return values.Select(e => new KeyValuePair<int, string>(Convert.ToInt32(e), e.GetDescription())).ToList(); }
DisplayMember
and ValueMember
properties:comboBox1.DataSource = GetEnumValues<Gender>(); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key";
In this example, we define an Enum
named Gender
with values and descriptions using the Description
attribute. We then create a method that returns the list of Enum values with their description using the GetDescription()
extension method that retrieves the description of the Enum value from the Description
attribute.
Finally, we bind the ComboBox with the list of Enum values using the DataSource
, DisplayMember
and ValueMember
properties. The DataSource
property is set to the list of Enum values, and the DisplayMember
and ValueMember
properties are set to "Value" and "Key" respectively to display the Enum descriptions and their corresponding values in the ComboBox.
Note that the GetDescription()
method is an extension method defined as follows:
public static string GetDescription(this Enum value) { var field = value.GetType().GetField(value.ToString()); var attribute = field .GetCustomAttributes(typeof(DescriptionAttribute), false) .FirstOrDefault() as DescriptionAttribute; return attribute != null ? attribute.Description : value.ToString(); }
This extension method is used to retrieve the Description
attribute of the Enum value if it exists, or return the string representation of the Enum value if the Description
attribute is not present.
"C# bind combobox to enum with description"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"WPF combobox bind to enum with description"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"Enum to combobox in C# with description attribute"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"Bind enum values to combobox in WinForms C#"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList(); comboBox.DisplayMember = "Description"; comboBox.ValueMember = "Value";
"C# WPF combobox enum description attribute"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"Combobox enum binding with description in C#"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList(); comboBox.DisplayMember = "Description"; comboBox.ValueMember = "Value";
"C# combobox enum description attribute example"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"Bind enum to combobox with description in Windows Forms"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.DataSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }).ToList(); comboBox.DisplayMember = "Description"; comboBox.ValueMember = "Value";
"C# combobox enum values with description"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
"Binding Enum to Combobox with Description in C# WPF"
var enumValues = Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>(); comboBox.ItemsSource = enumValues.Select(e => new { Value = e, Description = e.GetDescription() }); comboBox.DisplayMemberPath = "Description"; comboBox.SelectedValuePath = "Value";
mvn-repo git-diff heif excel-2007 geocoding unauthorized android-storage tags rounded-corners inline-styles