开发者

How do I get all controls inside a specific RowDefinition/ColumnDefinition in a Grid?

I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all controls in a cont开发者_运维百科ainer.

Any tip? Thanks.


There's no way to do it without iterating all children. Here's an extension method that returns only the children in a specific grid position :

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


Sorry, there's no way to do this except to iterate over the children of the Grid and extract the values from the attached properties yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜