I am binding Enum values to combobox using this code,
xaml.cs:
ObservableCollection
public ObservableCollection
{
get { return genders; }
set { genders = value; }
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Type typegender = typeof(Gender);
FieldInfo[] arrGenderFieldValues = typegender.GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (var gen in arrGenderFieldValues)
{
Genders.Add(gen.GetValue(null).ToString());
}
this.DataContext = this;
}
xaml :-
now i have two 2 questions,
1. how to assign values
Employee emp= new Employee();
emp.Gender= ?
2. IS there any other possible way to bind enum values to combo box
please reply for this problems.....
Sanjay DholakiyaPosted Feb 27, 2014, 7:36 PM
Hi,
I don't think Enum is useful to load as a list in combo or grid. Normally, we can use Enum to increase readability and maintainability of our code. Therefore, we can use it in if conditions to compare with some meaning full enum name instead of numbers.
I would recommend here not to use enum to load as a list in dropdown but create a class with id and name in it. And then prepare a list of that class to bind with combo.
- Sanjay Dholakiya