Although it's easy to assign UIElements to rows and columns using the Grid.SetRow/Column attached property setters, there is no API that allows you to ask the grid what elements exist at a given location.
This extension method uses a simple linq expression to find any child elements of the grid that occupy the specified row and column.
public static Collection<TElement> GetElements<TElement>(this Grid grid, int row, int column)
where TElement : UIElement
{
var elements = from UIElement element in grid.Children
where element is TElement &&
Grid.GetRow(element) == row &&
Grid.GetColumn(element) == column
select element as TElement;
return new Collection<TElement>(elements.ToList());
}
2 comments:
Wow, thanks. This helped alot. I was simply using a for each loop before but this is a better option.
Very nice, ill be using this a fair bit I imagine...
Post a Comment