【问题标题】:why DataGridColumn.GetCellContent (DatagridRow) is always null even though DataGridRow is not null?为什么 DataGridColumn.GetCellContent (DatagridRow) 始终为空,即使 DataGridRow 不为空?
【发布时间】:2015-05-22 10:22:37
【问题描述】:

我试图在加载DataGridRow 时访问每个单元格内容,但DataGridColumn.GetCellContent(DataGridRow) 始终为空,尽管DataGridRow 中有该列的数据。 例如:

    void DataGrid_LoadingRow(object sender,DataGridRowEventArgs e)
    {
      foreach(var colItem in dg.Columns)
      {
        var cell=item.GetCellContent(e.Row as DataGridRow);//Row is not null, cell is null after this execution.       
      }
    }

可能是什么原因?

PS:我已禁用行和列的虚拟化。

谢谢

【问题讨论】:

  • 我使用了datagrid的updatelayout事件,可以访问datagridcell的内容。
  • 将您的评论添加为答案并将其标记以供其他用户查看并将此问题标记为已回答

标签: c# wpf wpf-controls wpfdatagrid


【解决方案1】:

DataGrid 是关于在DataGridRow 中加载行数据并填充一行时,起初它似乎在应用数据绑定等之前引发LoadingRow 事件。因此其DataGridCells 的内容将是此时为空。

它类似于PropertyChanging 事件(在应用更改之前触发)。而PropertyChanged 事件在该更改发生后触发。

您可以为此目的使用另一个事件。

【讨论】:

  • @kedarK 如果您认为这是一个真实的答案,请勾选它作为答案以帮助其他人知道。
  • 请注意,这在 Silverlight 中有所不同。在 Silverlight 中,您可以获取有关此事件的网格单元格内容。
  • 我们应该使用哪个事件?
  • 这个问题实际上是如何解决的?我们可以使用哪个事件来知道单元格的内容已经加载,以便我们可以获取它们?
  • @Christian Findlay 这取决于每个具体情况。因为它根据 WPF DataGrid 使用场景和选项而有所不同(特别是行和列虚拟化与可见性选项混合会影响此行为)。您必须测试其他事件(考虑 '...ed' 事件而不是 '...ing' 事件。或者稍后会发生的事件)。我认为这很简单。我现在没有时间在所有使用场景中为您测试一个安全的适当事件……对不起。
【解决方案2】:

这是一个软件错误,所以:

  • 将列可见性更改为可见
  • 删除DataGridTemplateColumn

它会很好用。

【讨论】:

    猜你喜欢
    • 2018-01-23
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 2010-11-30
    • 1970-01-01
    相关资源
    最近更新 更多