Question

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

Any tip? Thanks.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow