也許這是爲時已晚,我一直在看我的屏幕太久,但這真的讓我難住。我有datagrid顯示數據,但我加入了2個表後試圖顯示數據。數據在那裏,因爲我仍然可以使鼠標雙擊事件工作,但沒有文本顯示在任何列或行中。WPF Datagrid在行中有數據,但不顯示文本
這是XAML。
<DataGrid Grid.Column="1" Grid.Row="1" Name="ProjectsSubGrid" ItemsSource="{Binding}"
AutoGenerateColumns="False" IsReadOnly="True"
MouseDoubleClick="ProjectsSubGrid_MouseDoubleClick"
Initialized="ProjectsSubGrid_Initialized">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ProjectName}" Header="Project Name"/>
<DataGridTextColumn Binding="{Binding Path=AllottedHours}" Header="Allotted Hours"/>
<DataGridTextColumn Binding="{Binding Path=InvoicedHours}" Header="Invoiced Hours"/>
<DataGridTextColumn Binding="{Binding Path=UninvoicedHours}" Header="Uninvoiced Hours"/>
<DataGridTextColumn Binding="{Binding Path=RemainingHours}" Header="Remaining Hours"/>
</DataGrid.Columns>
</DataGrid>
這是我設置DataContext的C#代碼。
private void ProjectsSubGrid_Initialized(object sender, EventArgs e)
{
var list = from p in ProjectManagement.Context.Project
join a in ProjectManagement.Context.Account
on p.AccountId equals a.AccountId
select new ProjectView(){
AccountId = a.AccountId,
ProjectId = p.ProjectId,
ProjectName = p.ProjectName,
InvoicedHours = p.InvoicedHours,
AccountName = a.CompanyName,
AllottedHours = p.AllottedHours,
RemainingHours = p.RemainingHours,
UninvoicedHours = p.UninvoicedHours
};
ProjectsSubGrid.DataContext = list;
}
它適用於我使用下面的代碼,但只要我切換到它打破的連接表。
ProjectsSubGrid.DataContext = ProjectManagement.Context.Project.Where(p=>true);
任何幫助將不勝感激。
不要使用Initilized事件,更好地做到這一點在窗口的構造函數,然後檢查。 – AnjumSKhan