WPF Datagrid 在行中有数据但不显示文本
WPF Datagrid has data in the rows but not displaying text
也许是晚了,我一直在看我的屏幕太久了,但这真的让我难住了。我有显示数据的数据网格,但在我加入 2 个表并尝试显示数据之后。数据在那里,因为我仍然能够让鼠标双击事件起作用,但没有文本显示在任何列或行中。
这是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);
如有任何帮助,我们将不胜感激。
1) 你应该只在LoadGrid
方法中设置DataGrid
的DataContext
。
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ProjectsSubGrid.DataContext = list;
}));
2) 设置DataGridTextColumn
的Name属性没有意义。
3) 将您的 linq 查询更新为
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
}).ToList();
尝试将路径添加到您的绑定 DataGridTextColumn 语句。
这是我之前项目的一个例子。
<DataGrid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Addresses}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
SelectionMode="Single"
IsEnabled="{Binding IsSelectionEnabled}"
SelectedItem="{Binding SelectedAddress}"
SelectedIndex="{Binding SelectedIndex}"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Street}" Header="Street"/>
<DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
<DataGridTextColumn Binding="{Binding Path=PostalCode}" Header="PostalCode"/>
<DataGridTextColumn Binding="{Binding Path=City}" Header="City"/>
<DataGridTextColumn Binding="{Binding Path=Provence}" Header="Provence"/>
<DataGridTextColumn Binding="{Binding Path=Country}" Header="Country"/>
</DataGrid.Columns>
</DataGrid>
在此感谢大家的帮助。估计是我昨晚看屏幕时间太长了。我没有发布 ProjectView class 的代码,这就是问题所在。
我写了
public class ProjectView
{
public int ProjectId;
public string ProjectName;
public int AccountId;
public string AccountName;
public int AllottedHours;
public int InvoicedHours;
public int UninvoicedHours;
public int RemainingHours;
}
我通过将其更改为
来修复它
public class ProjectView
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public int AccountId { get; set; }
public string AccountName { get; set; }
public int AllottedHours{ get; set; }
public int InvoicedHours { get; set; }
public int UninvoicedHours { get; set; }
public int RemainingHours { get; set; }
}
也许是晚了,我一直在看我的屏幕太久了,但这真的让我难住了。我有显示数据的数据网格,但在我加入 2 个表并尝试显示数据之后。数据在那里,因为我仍然能够让鼠标双击事件起作用,但没有文本显示在任何列或行中。
这是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);
如有任何帮助,我们将不胜感激。
1) 你应该只在LoadGrid
方法中设置DataGrid
的DataContext
。
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ProjectsSubGrid.DataContext = list;
}));
2) 设置DataGridTextColumn
的Name属性没有意义。
3) 将您的 linq 查询更新为
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
}).ToList();
尝试将路径添加到您的绑定 DataGridTextColumn 语句。 这是我之前项目的一个例子。
<DataGrid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Addresses}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
SelectionMode="Single"
IsEnabled="{Binding IsSelectionEnabled}"
SelectedItem="{Binding SelectedAddress}"
SelectedIndex="{Binding SelectedIndex}"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Street}" Header="Street"/>
<DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
<DataGridTextColumn Binding="{Binding Path=PostalCode}" Header="PostalCode"/>
<DataGridTextColumn Binding="{Binding Path=City}" Header="City"/>
<DataGridTextColumn Binding="{Binding Path=Provence}" Header="Provence"/>
<DataGridTextColumn Binding="{Binding Path=Country}" Header="Country"/>
</DataGrid.Columns>
</DataGrid>
在此感谢大家的帮助。估计是我昨晚看屏幕时间太长了。我没有发布 ProjectView class 的代码,这就是问题所在。
我写了
public class ProjectView
{
public int ProjectId;
public string ProjectName;
public int AccountId;
public string AccountName;
public int AllottedHours;
public int InvoicedHours;
public int UninvoicedHours;
public int RemainingHours;
}
我通过将其更改为
来修复它public class ProjectView
{
public int ProjectId { get; set; }
public string ProjectName { get; set; }
public int AccountId { get; set; }
public string AccountName { get; set; }
public int AllottedHours{ get; set; }
public int InvoicedHours { get; set; }
public int UninvoicedHours { get; set; }
public int RemainingHours { get; set; }
}