【问题标题】:Binding within DataGridTemplateColumn在 DataGridTemplateColumn 内绑定
【发布时间】:2012-11-10 19:11:33
【问题描述】:

我有 academic lectrurers 集合,它是使用 Entity Framework 从数据库中获取的。

LecturersLecturerListViewModel 中的一个属性:

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 绑定在代码隐藏中。

【问题讨论】:

    标签: wpf entity-framework binding lazy-loading selecteditem


    【解决方案1】:

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

    【讨论】:

    • 嘿!即使没有实现INotifyPropertyChanged,绑定到Name 也有效。只是ComboBox 不能正常工作。我已将Lecturer 类更改为this version,但ComboBoxes 中仍然没有选定值。怎么了?
    • @patryk.beza:有太多事情可能出错了,通知只是最常见的原因之一......
    【解决方案2】:

    如果要将枚举值绑定到组合框,则必须提供带有枚举值的 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}}" />
     ...
     ...
    

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 2011-11-27
      • 2013-07-02
      • 2017-01-16
      • 2011-04-06
      • 2014-04-14
      • 1970-01-01
      • 2012-10-13
      • 2012-04-07
      相关资源
      最近更新 更多