WPF Datagrid在行中有数据但不显示文本

时间:2015-12-02 07:01:59

标签: c# linq datagrid wpfdatagrid datacontext

也许已经很晚了,我一直在看我的屏幕太长时间,但这确实让我难过。我有数据网格显示数据,但在我加入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);

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

感谢大家的帮助。我想这只是我昨晚在屏幕上看了太久。我没有发布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; }
}

答案 1 :(得分:0)

1)您只应在DataContext方法中设置DataGrid的{​​{1}}。

LoadGrid

2)设置this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { ProjectsSubGrid.DataContext = list; })); 的Name属性没有意义。 3)将linq查询更新为

DataGridTextColumn

答案 2 :(得分:0)

尝试将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>