本文介绍了获取数据网格中的所有单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法可以获取DataGrid中所有单元的可迭代集合,而不管是否已选中它们
Is there a way to get an iteratable collection of all the cells in a DataGrid regardless of whether they are selected or not
推荐答案
如果您的意思是 DataGridCell
s,则可以使用Vincent Sibals帮助函数遍历 DataGrid.Items
的所有行和列 DataGrid.Columns
。
If you mean DataGridCell
s you could use Vincent Sibals helper functions to iterate over all rows DataGrid.Items
and columns DataGrid.Columns
.
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
DataGrid_Standard.ScrollIntoView(rowContainer, DataGrid_Standard.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
DataGrid_Standard.ScrollIntoView(DataGrid_Standard.Items[index]);
row = (DataGridRow)DataGrid_Standard.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
编辑
如果网格是您的DataGrid,则将获得所有DataGridCell的列表,如下所示:
If grid is your DataGrid you get a list of all DataGridCells like this:
List<DataGridCell> allCellList = new List<DataGridCell>();
for (int i = 0; i < grid.Items.Count; i++)
{
for (int j = 0; j < grid.Columns.Count; j++)
{
allCellList.Add(grid.GetCell(i, j));
}
}
这篇关于获取数据网格中的所有单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!