Andre Pena
Andre Pena

Reputation: 59336

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 container.

Any tip? Thanks.

Upvotes: 0

Views: 1832

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292465

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

Upvotes: 1

Drew Marsh
Drew Marsh

Reputation: 33379

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.

Upvotes: 1

Related Questions