tags:

views:

69

answers:

2

I need to get all controls inside a specific RowDefinition/ColumnDefinition without iterating through all controls in a container.

Any tip? Thanks.

+1  A: 

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.

Drew Marsh
Yes, I was trying this for 20 minutos, was already losing hope. Thanks anyway.
Ciwee
+1  A: 

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