卖萌的酱牛肉 · java8如何把list分组 | ...· 2 月前 · |
率性的火锅 · 使用亚马逊 S3 作为来源 AWS DMS ...· 2 月前 · |
谈吐大方的小蝌蚪 · A-Z Databases: ...· 4 月前 · |
苦恼的冲锋衣 · 【完本】斗罗大陆同人 女主小舞重生暴虐唐三 ...· 6 月前 · |
鼻子大的墨镜 · 在汤加失联的中国人最新消息!@赵立坚的评论区 ...· 6 月前 · |
寂寞的绿豆
2 月前 |
WPF DataGrid是一种用于显示和编辑数据的强大控件。要检测可见单元格的集合,可以使用以下步骤:
VisualTreeHelper.GetChildrenCount
和
VisualTreeHelper.GetChild
方法。
ItemContainerGenerator.ContainerFromIndex
方法获取DataGridRow对象。
ItemContainerGenerator.ContainerFromIndex
方法获取DataGridCellsPresenter对象。
ItemContainerGenerator.ContainerFromIndex
方法获取DataGridCell对象。
以下是示例代码:
private List<DataGridCell> GetVisibleCells(DataGrid dataGrid)
List<DataGridCell> visibleCells = new List<DataGridCell>();
DataGridCellsPresenter cellsPresenter = FindVisualChild<DataGridCellsPresenter>(dataGrid);
if (cellsPresenter != null)
for (int i = 0; i < dataGrid.Items.Count; i++)
DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
if (row != null)
DataGridCellsPresenter rowCellsPresenter = FindVisualChild<DataGridCellsPresenter>(row);
if (rowCellsPresenter != null)
for (int j = 0; j < dataGrid.Columns.Count; j++)
DataGridCell cell = (DataGridCell)rowCellsPresenter.ItemContainerGenerator.ContainerFromIndex(j);
if (cell != null && cell.Visibility == Visibility.Visible)
visibleCells.Add(cell);
return visibleCells;
private T FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
return (T)child;
T result = FindVisualChild<T>(child);
if (result != null)
return result;
return null;
}
这样,
GetVisibleCells
方法将返回一个包含所有可见单元格的集合。你可以根据需要进一步处理这些单元格。
对于WPF DataGrid的更多信息和使用示例,你可以参考腾讯云的WPF DataGrid产品介绍页面: WPF DataGrid产品介绍 。