在datagrid中获取行

我试着像这样排:

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock; 

但我只是null 。 还有其他解决方案吗? 我究竟做错了什么?

我想从我的细胞中获取数据。 我的单元格是复选框。

这取决于您尝试获取此数据的方式/时间。 WPF更适合通过ItemsSource中绑定的对象访问数据。 因此,如果您的ItemsSource是MyObject的List,那么特定的行将是MyObject类型而不是纯DataRow。

如果您通过单击来访问数据,则可以执行以下操作:

 var currentItem = myDataGrid.SelectedItem as MyObject; 

现在,您拥有当前MyObject的最初预期forms,而不是在网格中拾取。

  for(int row =0; row < dg_CountInventory.Rows.Count; row ++) //Loop through each row { //Provide the Column Index and row as in Loop TextBlock b = dg_CountInventory.Columns[1].GetCellContent(dg_CountInventory.Items[row ]) as TextBlock; } 

dg_CountInventory是我的网格名称。此代码将遍历数据网格和单元格/列提供中的所有记录。#