How to programmatically access Control in WPF Grid by row and column index?

Mathias picture Mathias · Oct 2, 2009 · Viewed 52.3k times · Source

Once Controls have been added to a WPF Grid, is there a way to programmatically access them by row and/or column index? Something along the lines of:

 var myControl = (object)MyGrid.GetChild(int row, int column);

... where GetChild is the method I wish I had!

Answer

itowlson picture itowlson · Oct 2, 2009

There isn't a built-in method for this, but you can easily do it by looking in the Children collection:

myGrid.Children
      .Cast<UIElement>()
      .First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == column);