【问题标题】:WPF Datagrid has data in the rows but not displaying textWPF Datagrid 在行中有数据但不显示文本
【发布时间】:2016-03-06 08:08:25
【问题描述】:

也许是太晚了,我已经看屏幕太久了,但这真的让我很难过。我让数据网格显示数据,但在我加入 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);

任何帮助将不胜感激。

【问题讨论】:

  • 不要使用Initilized事件,最好在Window的构造函数中做,然后检查。

标签: c# linq datagrid wpfdatagrid datacontext


【解决方案1】:

1) 您应该只在LoadGrid 方法中设置DataGridDataContext

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();

【讨论】:

  • 我尝试了 refresh 和 updatelayout 以及在初始化页面时调用它,但都没有奏效。我什至删除了 DataGridTextColumn 的 Name 属性,不管你信不信这也没有用。
  • LoadGrid 方法中获得您的评论行ProjectsSubGrid.ItemsSource = list
  • 更新了我对第一点的回答。不要添加第 2 点中提到的行。
  • 那也没用。我尝试在初始化事件上运行它,但这也不起作用。如果我将 linq 查询切换回单个实体,它可以工作,但我需要第二个表中的一些列。
  • 好的。问题似乎是应该执行 .ToList() 来获取记录的 linq 查询。更新了我的答案,请检查。
【解决方案2】:

尝试将 Path 添加到您的绑定 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>

【讨论】:

  • 我猜你的 DataContext 和 Itemsource 绑定一定有问题。尝试 ItemsSource="{Binding list}" 然后可能尝试删除 ProjectsSubGrid.DataContext = list;您同时绑定了网格的 itemsource 和 dataContext 似乎很奇怪。 (编辑:按回车键快速)。
  • 忘了提及我的示例使用 MVVM,我想您是在代码后面做的。 here你可以找到与你的问题类似的东西。
  • 感谢您的链接。它没有回答我的问题,但它确实让我意识到了这个问题。我忘了使用访问器 {get; set;} 在 ProjectView 类成员上。
【解决方案3】:

在这里感谢大家的帮助。我猜是我昨晚看屏幕太久了。我没有发布 ProjectView 类的代码,这就是问题所在。

我写过

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多