如何在网格中的特定RowDefinition / ColumnDefinition中获取所有控件?

时间:2009-10-30 00:01:26

标签: c# .net wpf grid

我需要在特定的RowDefinition / ColumnDefinition中获取所有控件,而不必迭代容器中的所有控件。

任何提示?感谢。

2 个答案:

答案 0 :(得分:1)

抱歉,除了遍历网格的子节点并自己从附加属性中提取值之外,没有办法做到这一点。

答案 1 :(得分:1)

没有迭代所有孩子就没有办法做到这一点。这是一个只返回特定网格位置中的子项的扩展方法:

public static class GridExtensions
{
    public static IEnumerable<DependencyObject> GetChildren(this Grid grid, int row, int column)
    {
        int count = VisualTreeHelper.GetChildrenCount(grid);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(grid, i);
            int r = Grid.GetRow(child);
            int c = Grid.GetColumn(child);
            if (r == row && c == column)
            {
                yield return child;
            }
        }
    }
}
相关问题