This is the user control which I’m working on. The items in the first comboBox are gonna be the same in dataGridColumn GroupID.

The code to show in the first comboBox is
<ComboBox ItemsSource="{Binding}" Name="GroupComboBox" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" Grid.Column="1" Margin="5" />
private void LoadGroups()
{
NorthwindDataContext dc = new NorthwindDataContext();
var groups = (from p in dc.Group
select p);
this.DataContext = groups;
}
private void LoadStudents()
{
NorthwindDataContext dc = new NorthwindDataContext();
var students = (from p in dc.Student
select p);
dataGrid1.ItemsSource = students;
}
But in the another comboBox, don’t appear any item in it.
<DataGridTemplateColumn Header="GroupID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
How can I bind it?
I was thinking in put all the Groups in a list, but I’m not sure it’ll be a good way because I’d need to convert my query in a list.
UPDATE 1:
I had to remove this line:
<DataGridComboBoxColumn Header="GroupID" ItemsSource="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" />
for
<DataGridTemplateColumn Header="GroupID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" SelectedValue="{Binding GroupID}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The binding of your internal ComboBox will not work because the DataContext of that ComboBox is your
Groupscollection but one of theStudents, this means setting theItemsSourceto{Binding}will make theStudenttheItemsSource.You can navigate up the Tree with a RelativeSource binding to a place where the inherited
DataContextis still theGroupscollection, e.g. if the inheritance is not blocked by setting theDataContextinbetween this will work: