Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么DataGridColumn.GetCellContent(DatagridRow)始终为null,即使DatagridRow不为null?_C#_Wpf_Wpf Controls_Wpfdatagrid - Fatal编程技术网

C# 为什么DataGridColumn.GetCellContent(DatagridRow)始终为null,即使DatagridRow不为null?

C# 为什么DataGridColumn.GetCellContent(DatagridRow)始终为null,即使DatagridRow不为null?,c#,wpf,wpf-controls,wpfdatagrid,C#,Wpf,Wpf Controls,Wpfdatagrid,当加载DataGridRow时,我试图访问每个单元格内容,但是DataGridColumn.GetCellContent(DataGridRow)始终为空,尽管DataGridRow中有该列的数据。 例如: 原因可能是什么 PS:我已经禁用了行和列的虚拟化 感谢当DataGrid要在DataGridRow中加载行数据并填充行时,它似乎首先在应用数据绑定等之前引发LoadingRow事件。因此其DataGridCell的内容此时将为空 它类似于PropertyChanging事件(在应用更改之前触

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

原因可能是什么

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


感谢

DataGrid
要在
DataGridRow
中加载行数据并填充行时,它似乎首先在应用数据绑定等之前引发
LoadingRow
事件。因此其
DataGridCell
的内容此时将为空

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


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

这是一个软件错误,因此:

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

它会很好地工作。

我使用了datagrid的updatelayout事件,并且我能够访问datagridcell内容。添加您的评论作为答案,并将其标记为其他用户可以查看,并将此问题标记为answered@kedarK如果你认为这是一个真实的答案,勾选它作为帮助其他人了解的答案。请注意,这在Silverlight中是不同的。在Silverlight中,您可以获取此事件的网格单元格内容。我们应该使用哪个事件?此问题实际上是如何解决的?我们可以使用哪个事件来知道单元格的内容已加载,以便我们能够获取它们?@Christian Findlay这取决于每个具体情况。因为它根据WPF DataGrid的使用场景和选项而有所不同(特别是行和列虚拟化与可见性选项的混合会影响此行为)。您必须测试其他事件(考虑“…ed”事件,而不是“…ing”事件。或稍后发生的事件)。我认为这很简单。我现在没有时间在所有使用场景中为您测试安全合适的事件。。。驱逐我。
    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.       
      }
    }