简体   繁体   English

WPF DataGrid 绑定到导航属性

[英]WPF DataGrid binding to navigation property

I'm using Entity Framework 6 in Visual Studio 2015 to create a WPF app with a DataGrid.我在 Visual Studio 2015 中使用 Entity Framework 6 来创建一个带有 DataGrid 的 WPF 应用程序。 It needs to bind to search results that contain Entity Framework navigation properties 3 levels deep: An Employee can have any number of EmployeeStatus , and an EmployeeStatus can have an EmployeeStatusDescription entity.它需要绑定到包含实体框架导航属性 3 级深度的搜索结果:一个 Employee 可以有任意数量的EmployeeStatus ,一个EmployeeStatus可以有一个EmployeeStatusDescription实体。 So it looks like this:所以它看起来像这样:

Employee > EmployeeStatus > EmployeeStatusDescription员工 > 员工状态 > 员工状态描述

I have the following Include()我有以下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); 

I try to bind the EmployeeStatusDescription like this:我尝试像这样绑定 EmployeeStatusDescription:

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

But Status column comes up blank for an Employee that has an EmployeeStatus and an associated navigation property EmployeeStatusDescription .但是对于具有EmployeeStatus和关联导航属性EmployeeStatusDescriptionEmployeeStatus列显示为空白。

However, on the database, the following SQL brings back the EmployeeStatusDescription just fine:但是,在数据库上,以下 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'

What am I doing wrong?我究竟做错了什么? Thanks for your help.谢谢你的帮助。

Update 1 : EmployeeStatus is defined in my Employee model like this, so it's a collection, not just a single value:更新 1 : EmployeeStatus 在我的 Employee 模型中定义如下,因此它是一个集合,而不仅仅是一个值:

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

Update 2 : Here's the EmployeeStatusDescription class:更新 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; }
}

If EmployeeStatus is a collection, you won't be able to just say EmployeeStatus.EmployeeStatusDescription.description.如果 EmployeeStatus 是一个集合,您将不能只说 EmployeeStatus.EmployeeStatusDescription.description。 .NET doesn't know which one to spit back. .NET 不知道要回吐哪一个。 You could create an IValueConverter that takes in a collection and returns the first.您可以创建一个 IValueConverter 接受一个集合并返回第一个。

Or, you could create a property on your viewmodel that returns the first item from the EmployeeStatus collection或者,您可以在视图模型上创建一个属性,该属性返回 EmployeeStatus 集合中的第一项

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

Or you could add a content control that would be bound to all the items in the EmployeeStatus collection and list all of them.或者,您可以添加一个内容控件,该控件将绑定到 EmployeeStatus 集合中的所有项目并列出所有项目。

Edit To show all the values inside the datagrid for the employee, you'll need another control to iterate the collection.编辑要显示员工数据网格中的所有值,您需要另一个控件来迭代集合。 Inside the column that would show the EmployeeStatus, you'll need to make it a template column and add a control that can do the iteration:在将显示 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>

Items Control Binding项目控制绑定

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM