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());
}
Wow, thanks. This helped alot. I was simply using a for each loop before but this is a better option.
ReplyDeleteVery nice, ill be using this a fair bit I imagine...
ReplyDeleteWonderfull. I was going mad trying to find this in WPF, but it's simply not built in ...
ReplyDeleteThanks for the post it helped me get to where I wanted.
ReplyDeleteI have a table with a check box column that I wanted to check all and uncheck all according to top check box value.
Your post helped me get to the right row/column for the check box elements.
(I have posted a link to your blog from my post here)