How do I get all controls inside a specific RowDefinition/ColumnDefinition in a Grid?
https://stackoverflow.com/questions/1647265
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;
}
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow