WPF Datagrid with ComboBox Template
I have the below Person Model as an example class, with an ObservableCollection<string> of occupations. How would I bind a ComboBox so that the ItemSource is linked to the Occupations property, and the selected item is linked to the Occupation property? This is usually pretty easy with the standard data grid, but is causing me issues with the Xceed version and I haven't been able to figure it out through testing these past few days.
It may be worth noting that my Datagrid is bound to the ObservableCollection<PersonModel> People in the view model.
- public class PersonModel : INotifyPropertyChanged
- {
- #region <--Constructor and Destructor...-->
- public PersonModel()
- {
- Occupations.Add("Engineer");
- Occupations.Add("Farmer");
- Occupations.Add("Politician");
- }
- ~PersonModel() { } //Currently Does Nothing
- #endregion
- #region <--Fields...-->
- private string firstName;
- private string lastName;
- private string occupation;
- private static ObservableCollection<string> occupations = new ObservableCollection<string>() { "Engineer", "Farmer", "Politician", "Lawyer", "Coder", "Teacher" };
- #endregion
- #region <--Property Changed Event Handlers...-->
- public event PropertyChangedEventHandler PropertyChanged;
- // This method is called by the Set accessor of each property.
- // The CallerMemberName attribute that is applied to the optional propertyName
- // parameter causes the property name of the caller to be substituted as an argument.
- private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- #endregion
- #region <--Properties...-->
- public ObservableCollection<string> Occupations
- {
- get => occupations;
- set => occupations = value;
- }
- public string Occupation
- {
- get { return occupation; }
- set
- {
- occupation = value;
- NotifyPropertyChanged(nameof(Occupation));
- }
- }
- public string FullName
- {
- get => LastName + ", " + FirstName;
- }
- public string LastName
- {
- get { return lastName; }
- set
- {
- lastName = value;
- NotifyPropertyChanged(nameof(LastName));
- }
- }
- public string FirstName
- {
- get { return firstName; }
- set
- {
- firstName = value;
- NotifyPropertyChanged(nameof(FirstName));
- }
- }
- #endregion
- #region <--Methods...-->
- #endregion
- }