这是我正在处理的用户控件。 dataGridColumn GroupID中的第一个组合框中的项目将会相同。如何将数据绑定到DataGrid内部的ComboBox?
的代码在第一组合框显示是
<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;
}
但在另一个组合框,不要在它出现的任何项目。
<DataGridTemplateColumn Header="GroupID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我该如何绑定它?
我在想把所有的组放在一个列表中,但我不确定它会是一个好方法,因为我需要将列表中的查询转换成列表。
更新1:
,我不得不删除这条线:
<DataGridComboBoxColumn Header="GroupID" ItemsSource="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValuePath="GroupID" DisplayMemberPath="GroupName" />
为
<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>
我会建议使用WPF重新标记为不同的UI是非常不同的,当涉及到结合。谢谢。 – Jay
编辑此帖子上的标签以包含WPF,您可能还想删除数据库和/或linq。恰当地标记你的问题对于吸引正确的专家到你的文章非常重要。 – Jay
Ohh没错,我忘了最重要的标签 –