【问题标题】:WPF DataGrid binding to navigation propertyWPF DataGrid 绑定到导航属性
【发布时间】:2016-03-28 12:42:16
【问题描述】:

我在 Visual Studio 2015 中使用 Entity Framework 6 来创建一个带有 DataGrid 的 WPF 应用程序。它需要绑定到包含 3 级深度的实体框架导航属性的搜索结果:一个员工可以有任意数量的EmployeeStatus,一个EmployeeStatus 可以有一个EmployeeStatusDescription 实体。所以它看起来像这样:

员工 > 员工状态 > 员工状态描述

我有以下Include()

var comparison = Expression.Lambda<Func<Employee, bool>>(
    Expression.Equal(
        Expression.Property(paramEmployee, selectedColumnValue),
        Expression.Constant(SearchValue)), 
        paramEmployee).Compile();

var query = (from e in Context.Employees
             .Include("EmployeeStatus.EmployeeStatusDescription")
             .Where(comparison)
             select e); 

我尝试像这样绑定 EmployeeStatusDescription:

<DataGrid ItemsSource="{Binding SearchResults}">
    <!-- other columns here... -->
    <DataGridTextColumn Binding="{Binding 
      EmployeeStatus.EmployeeStatusDescription.description}" Header="Status" />
</DataGrid>

但是对于具有EmployeeStatus 和关联导航属性EmployeeStatusDescriptionEmployeeStatus 列显示为空白。

但是,在数据库上,下面的 SQL 带回 EmployeeStatusDescription 就好了:

select esd.* from employee e
    left join EmployeeStatus es on e.employeeID = es.employeeID
    left join EmployeeStatusDescription esd on 
       es.employeeStatusDescriptionID = esd.employeeStatusDescriptionID
where e.employeeID = '30299'

我做错了什么?感谢您的帮助。

更新 1:EmployeeStatus 在我的 Employee 模型中是这样定义的,所以它是一个集合,而不仅仅是一个值:

public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }

更新 2:这是 EmployeeStatusDescription 类:

public partial class EmployeeStatusDescription
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public EmployeeStatusDescription()
    {
        this.EmployeeStatus = new HashSet<EmployeeStatu>();
    }

    public int employeeStatusDescriptionID { get; set; }
    public string employeeStatusAbbreviation { get; set; }
    public string description { get; set; }
    public bool isActive { get; set; }
    public System.DateTime createdDate { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
}

【问题讨论】:

  • EmployeeStatus 对象上的 EmployeeStatusDescription 属性是刚刚称为 EmployeeStatusDescription 还是其他名称?

标签: c# wpf entity-framework data-binding datagrid


【解决方案1】:

如果 EmployeeStatus 是一个集合,您将不能只说 EmployeeStatus.EmployeeStatusDescription.description。 .NET 不知道要吐回哪一个。您可以创建一个接收集合并返回第一个的 IValueConverter。

或者,您可以在视图模型上创建一个属性,该属性返回 EmployeeStatus 集合中的第一项

    public EmployeeStatus FirstStatus{
       get { if (EmployeeStatus != null) { return EmployeeStatus.First(); } return null; }
    }

或者您可以添加一个内容控件,该控件将绑定到 EmployeeStatus 集合中的所有项目并列出所有项目。

编辑 要显示员工数据网格中的所有值,您需要另一个控件来迭代集合。在显示 EmployeeStatus 的列中,您需要将其设为模板列并添加一个可以进行迭代的控件:

<DataGridTemplateColumn Header="Employee Status">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <ListBox ItemsSource="{Binding Source=EmployeeStatus}">
                   <ListBox.ItemTemplate>
                       <DataTemplate>
                           <TextBlock Text="{Binding EmployeeStatusDescription.description}" />
                      </DataTemplate>
                   </ListBox.ItemTemplate>
                </ListBox>
          </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

项目控制绑定

<DataGridTemplateColumn Header="Employee Status">
         <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                    <ItemsControl ItemsSource="{Binding EmployeeStatus}">
                         <ItemsControl.ItemTemplate>
                             <DataTemplate>
                                  <TextBlock Text="{Binding EmployeeStatusDescription.description}" />
                             </DataTemplate>
                         </ItemsControl.ItemTemplate>
                     </ItemsControl>
                <DataTemplate>
           <DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>

【讨论】:

  • 谢谢,@Josh。如何为查询中的每个员工返回 EmployeeStatus 集合及其关联的 EmployeeStatusDescriptions?
  • 谢谢,@Josh。它在 Status 列中返回一堆空白 ListBox 元素。此外,Intellisense 无法识别 ListBox 控件内的Source=EmployeeStatus,也无法识别EmployeeStatusDescription.description
  • Intellisense 不会在设计时选择它。尝试将 EmployeeStatus 更改为 ObservableCollection。发布您的 EmployeeStatusDescription 类。
  • 请看上面,@Josh。
  • 谢谢,@Josh。得到它的工作!我不得不从&lt;ListBox ItemsSource="{Binding Source=EmployeeStatus}"&gt; 中删除Source=。不要问我为什么。我以前也遇到过这种情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2010-12-01
  • 2013-02-28
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多