【发布时间】:2011-02-15 17:46:07
【问题描述】:
在我的 WPF 4 桌面应用程序中,我有一个 LINQ 查询,它与两个表进行连接。
这是返回 LINQ 查询结果的函数代码:
public IList GetTableData()
{
IList dataList = (from dWorker in App.glidusContext.tbl_workers
join d2 in App.glidusContext.tbl_workers_has_tbl_events
on dWorker.workerID equals d2.workerID
where dWorker.workerTZ == strSplit
select new { dWorker, d2 }).ToList();
return data list;
}
现在,我剩下要做的就是将此 LINQ 查询的数据绑定到DataGrid:
private IList currentData; //class property
public void AssignTableContentToDataContext()
{
currentData = GetTableData();
this.ContentDataGrid.DataContext = currentData;
}
这是 DataGrid 的 XAML 代码:
<DataGrid x:Name="ContentDataGrid"
Style="{StaticResource Body_Content_DataGrid}"
CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
ItemsSource="{Binding}"
KeyboardNavigation.TabIndex="8" SelectionChanged="ContentDataGrid_SelectionChanged" DataContext="{Binding}">
<!-- PreviewKeyDown="DBRecord_Delete_PreviewKeyDown" -->
<!-- CellEditEnding="ContentDataGrid_CellEditEnding" -->
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerID}"
Width="130"
IsReadOnly="True"
Binding="{Binding Path=workerID}" />
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_workerTZ}"
Width="130"
Binding="{Binding Path=workerTZ}" />
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerFirstName}"
Width="130"
Binding="{Binding Path=workerFirstName}" />
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerLastName}"
Width="130"
Binding="{Binding Path=workerLastName}" />
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EventID}"
Width="130"
Binding="{Binding Path=eventID}" />
<DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EventName}"
Width="*"
Binding="{Binding Path=eventName}" />
</DataGrid.Columns>
</DataGrid>
调试后得到以下结果:
- 我的查询返回带有结果的行
- 我的
DataGrid根据查询结果中的行数显示行,但是它们是空白的!
那么我怎样才能对这些数据进行数据绑定并在DataGrid中显示呢?
附:对于更简单的查询,例如select * from _table_,一切正常,DataGrid 显示数据,但是当我对多个表使用更复杂的查询(进行连接)时,我无法正确绑定它。
【问题讨论】:
标签: wpf linq data-binding datagrid join