我有学术讲师集合,它是使用Entity Framework从数据库中获取的。
Lecturers
是 内的属性LecturerListViewModel
:
public ObservableCollection<Lecturer> Lecturers { get; set; }
Lecturer
是数据库逆向工程的类:
public class Lecturer
{
public Lecturer()
{
}
public int Id_Lecturer { get; set; }
public string Name { get; set; }
public int? Academic_Degree_Id { get; set; }
public virtual AcademicDegree AcademicDegree { get; set; } // lazy loading
}
我想显示数据库中的所有讲师DataGrid
(网格的所有单元格都是可编辑的;当用户单击保存按钮时,所有数据都将保存在数据库中)。问题是将最近的学位绑定到组合框。我试过这样:
<StackPanel d:DataContext="{d:DesignInstance Type=ViewModel:LecturerListViewModel}">
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Lecturers}" HeadersVisibility="Column">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTemplateColumn Header="Academic degree">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<!-- PROBLEM HERE. SelectedItem="{Binding AcademicDegree}" doesn't work -->
<ComboBox SelectedItem="{Binding AcademicDegree}" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AcademicDegrees}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Add" HorizontalAlignment="Center" Width="100" Margin="15" Name="AddLecturerButton" />
</StackPanel>
ItemsSource="{Binding Lecturers}"
正确绑定。点击后有列表checkbox
。但是程序启动后,里面的值checkboxes
是空的。为什么?我该如何解决?
PS。LecturerListViewModel
绑定在代码隐藏中。