0

我有学术讲师集合,它是使用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绑定在代码隐藏中。

4

2 回答 2

0

您的对象需要通过 提供更改通知INotifyPropertyChanged。(因此,如果延迟加载完成,则需要为属性发出通知)

于 2012-11-10T19:14:22.630 回答
0

如果要将枚举值绑定到组合框,则必须提供带有枚举值的 IEnumerable。实现这一点的最简单方法是ObjectDataProvider在您的资源中定义一个。

 xmlns:System="clr-namespace:System;assembly=mscorlib"
 xmlns:MyEnums="clr-namespace:LocalNamespaceWhereAcademicDegreeIsDefined"    
 ...
 <Window.Resources>
     <ObjectDataProvider x:Key="AcademicDegrees" MethodName="GetValues" ObjectType="{x:Type System:Enum}" >
         <ObjectDataProvider.MethodParameters>
             <x:Type TypeName="MyEnums:AcademicDegree"/>
          </ObjectDataProvider.MethodParameters>
     </ObjectDataProvider>
 <Window.Resources>
 ...
 ...
 <ComboBox SelectedItem="{Binding AcademicDegree}" ItemsSource="{Binding Source={StaticResource AcademicDegrees}}" />
 ...
 ...
于 2012-11-10T19:41:16.213 回答