gpt4 book ai didi

wpf - 获取数据网格中的所有单元格

转载 作者:行者123 更新时间:2023-12-02 22:34:44 26 4
gpt4 key购买 nike

有没有办法获取 DataGrid 中所有单元格的可迭代集合,无论它们是否被选中

最佳答案

如果您指的是 DataGridCell,则可以使用 Vincent Sibals 辅助函数来遍历所有行 DataGrid.Items 和列 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;
}

编辑

如果 grid 是您的 DataGrid,您会得到一个包含所有 DataGridCell 的列表,如下所示:

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));
}
}

关于wpf - 获取数据网格中的所有单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614833/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com