private void combo_Loaded(object sender, RoutedEventArgs e) { ComboBox comboBox = (ComboBox)sender; CustomerViewModel customerViewModel = (CustomerViewModel)this.DataContext; Type _type = customerViewModel.GetType(); PropertyInfo _propertyInfo = _type.GetProperty(StatusName.ToString()); Status MyStatus = (Status)Enum.Parse(typeof(Status), StatusValue.ToString(), true); _propertyInfo.SetValue(customerViewModel, MyStatus); customerViewModel.VerifyPropertyName(StatusName); // Set initial Enum values to given Status Name and Value for (int index = 0; index < combo.Items.Count; index++) { EnumItem item = (EnumItem)combo.Items.GetItemAt(index); if (item.Value.ToString() == MyStatus.ToString()) { combo.SelectedItem = item; break; } } }
private void combo_SelectionChanged(object sender, SelectionChangedEventArgs e) { // Update DataContext ComboBox comboBox = (ComboBox)sender; CustomerViewModel customerViewModel = (CustomerViewModel)this.DataContext; Type _type = customerViewModel.GetType(); // verify if property name exists in model customerViewModel.VerifyPropertyName(StatusName); // use reflection to update selected value in data context PropertyInfo _propertyInfo = _type.GetProperty(StatusName.ToString()); EnumItem item = (EnumItem)combo.SelectedItem; Status MyStatus = (Status)Enum.Parse(typeof(Status), item.Value.ToString(), true); _propertyInfo.SetValue(customerViewModel, MyStatus); }