C# WPF DataGrid绑定到导航属性
我正在Visual Studio 2015中使用Entity Framework 6创建一个带有DataGrid的WPF应用程序。它需要绑定到包含实体框架导航属性的搜索结果3层:员工可以有任意数量的C# WPF DataGrid绑定到导航属性,c#,wpf,entity-framework,data-binding,datagrid,C#,Wpf,Entity Framework,Data Binding,Datagrid,我正在Visual Studio 2015中使用Entity Framework 6创建一个带有DataGrid的WPF应用程序。它需要绑定到包含实体框架导航属性的搜索结果3层:员工可以有任意数量的EmployeeStatus,而EmployeeStatus可以有EmployeeStatusDescription实体。看起来是这样的: <DataGrid ItemsSource="{Binding SearchResults}"> <!-- other columns
EmployeeStatus
,而EmployeeStatus
可以有EmployeeStatusDescription
实体。看起来是这样的:
<DataGrid ItemsSource="{Binding SearchResults}">
<!-- other columns here... -->
<DataGridTextColumn Binding="{Binding
EmployeeStatus.EmployeeStatusDescription.description}" Header="Status" />
</DataGrid>
员工>员工状态>员工状态描述
我有以下Include()
我做错了什么?谢谢你的帮助
更新1:EmployeeStatus在我的员工模型中定义如下,因此它是一个集合,而不仅仅是一个值:
public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
public虚拟ICollection 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; }
}
公共部分类EmployeeStatusDescription
{
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]
公共雇员状态描述()
{
this.EmployeeStatus=new HashSet();
}
public int-employeeStatusDescriptionID{get;set;}
公共字符串EmployeeStatus缩写{get;set;}
公共字符串说明{get;set;}
公共bool isActive{get;set;}
public System.DateTime createdDate{get;set;}
[System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2227:CollectionPropertiesShouldBreadOnly”)]
公共虚拟ICollection EmployeeStatus{get;set;}
}
如果EmployeeStatus是一个集合,则不能只说EmployeeStatus.EmployeeStatusDescription.description。NET不知道该吐回哪一个。您可以创建一个IValueConverter,它接收一个集合并返回第一个集合
或者,可以在viewmodel上创建一个属性,返回EmployeeStatus集合中的第一个项
public EmployeeStatus FirstStatus{
get { if (EmployeeStatus != null) { return EmployeeStatus.First(); } return null; }
}
或者,您可以添加一个内容控件,该控件将绑定到EmployeeStatus集合中的所有项,并列出所有项
编辑
要为员工显示datagrid中的所有值,需要另一个控件来迭代集合。在显示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
集合及其关联的EmployeeStatusDescription
s?谢谢,@Josh。它在Status列中返回一堆空白的ListBox元素。此外,Intellisense不识别ListBox控件内的Source=EmployeeStatus
,也不识别EmployeeStatusDescription。description
Intellisense在设计时不会拾取它。尝试将EmployeeStatus更改为ObservableCollection。发布你的EmployeeStatusDescription类。请参见上面的@Josh.Thank,@Josh。开始工作了!我不得不从
中删除Source=
。别问我为什么。我以前也遇到过这种情况。EmployeeStatus对象上的EmployeeStatusDescription属性只是称为EmployeeStatusDescription还是其他什么?
<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>