WPF中如何遍历打开所有窗口

可以创建一个动态数组
窗体show时要把一个标识放到数组里
close时在把array中的标识移除

遍历时就根据这个数组中的标识来遍历
在主窗体中遍历时要注意不能new那些要遍历的窗体
要把对象传过来才可以 不然是空的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF遍历DataGrid的所有单元格,可以使用以下方法: 1. 遍历行和列:可以使用DataGrid的Items和Columns属性来遍历所有行和列。然后可以使用VisualTreeHelper.GetChild()方法获取单元格元素,再进一步处理。 ```csharp for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); if (row != null) { for (int j = 0; j < dataGrid.Columns.Count; j++) { DataGridCell cell = (DataGridCell)row.ItemContainerGenerator.ContainerFromIndex(j); if (cell != null) { //处理单元格 } } } } ``` 2. 使用VisualTreeHelper遍历:可以使用VisualTreeHelper遍历DataGrid的子元素,找到所有单元格元素。 ```csharp for (int i = 0; i < dataGrid.Items.Count; i++) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i); if (row != null) { for (int j = 0; j < dataGrid.Columns.Count; j++) { DataGridCell cell = GetCell(dataGrid, i, j); if (cell != null) { //处理单元格 } } } } private DataGridCell GetCell(DataGrid dataGrid, int row, int column) { DataGridRow rowContainer = GetRow(dataGrid, row); if (rowContainer != null) { DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); if (presenter == null) { dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]); presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); } if (presenter != null) { DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); return cell; } } return null; } private DataGridRow GetRow(DataGrid dataGrid, int index) { DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { dataGrid.UpdateLayout(); dataGrid.ScrollIntoView(dataGrid.Items[index]); row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(index); } return row; } private T GetVisualChild<T>(Visual parent) where T : Visual { T child = default(T); for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { Visual visual = (Visual)VisualTreeHelper.GetChild(parent, i); child = visual as T; if (child == null) { child = GetVisualChild<T>(visual); } if (child != null) { break; } } return child; } ``` 以上两种方法,都可以遍历DataGrid的所有单元格,然后进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值