views:

98

answers:

1

how to get the control added at the specified row and column in window control Grid(not gridview or datagrid)? in C#

Suppose we know which row and column the control is located, how to get the control by grid row and column?

Thanks

+1  A: 

Well, you could do this with a LINQ query easily enough:

public IEnumerable<UIElement> GetElementsAtPosition(Grid grid, int row, int column)
{
    return grid.Children.OfType<UIElement>().Where(c => Grid.GetRow(c) == row && Grid.GetColumn(c) == column);
}

This needs to return an IEnumerable, as there can be multiple controls at a given row & column intersection. This is also not very reliable if you are doing Row/Column spanning (it won't catch the case where an element starts at a different row or column, and spans into it).

EDIT Thanks @wpfwannabe for the proper LINQ operator required to make this work. It still boggles my mind that UIElementCollection doesn't implement IEnumerable<UIElement>.

Abe Heidebrecht
Thanks.Shall I add using namespace System.Linq or ...?
I have the System.Linq, but still have trouble with grid.Children.Wherewhat I miss please? Thanks
System.Windows.Controls.UIElementCollection' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Windows.Controls.UIElementCollection'
wpfwannabe
Sorry about that, I always forget that collection doesn't implement IEnumerable<UIElement> Thanks @wpfwannabe for the solution.
Abe Heidebrecht