I have a DataGrid bound to a collection of Employees. Employee class has an EmployeeCountry of type Country. Country type consists of CountryId and CountryName.
I have the following XAML:
<DataGrid ItemsSource="{Binding EmployeeList}" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="CountryCombo2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.CountryList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DisplayMemberPath="CountryName" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The EmployeeList and CountryList are ObservableCollection properties on the ViewModel which is the DataContext for the Window containing the DataGrid. I am able to get the ComboBox populate with the CountryList.
Problem: I need to figure out how to set other properties of the ComboBox such as SelectedValuePath, SelectedItem, etc., so that each row of the DataGrid is correctly displaying the appropriate EmployeeCountry in the ComboBox. If the EmployeeCountry property of the Employee is NULL, then the ComboBox should have no item selected.
Update : I am also not able to add a new row to the DataGrid even though the CanUserAddRows property is set to true.