47

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!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Mathias
  • 15,191
  • 9
  • 60
  • 92

5 Answers5

76

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);
itowlson
  • 73,686
  • 17
  • 161
  • 157
  • 7
    Although - It might be worth returning the full collection, since technically, you can have more than one element in a single grid "cell", since hte attached properties don't check for that. – Reed Copsey Oct 02 '09 at 21:16
  • Thank you. That is the approach I have been following so far, iterating over every child in Children until I find one control with matchin row and column, but I expected there would be something more direct. – Mathias Oct 02 '09 at 21:17
  • 3
    Thanks, Reed, good point -- that can easily be accomplished by using Where instead of First. – itowlson Oct 02 '09 at 21:19
  • worked. thank you. you can cast and use.for example:((Label)myGrid.Children[0]).Text="test"; – Ali Rasouli May 21 '19 at 20:09
12

This answer will help you

int rowIndex = Grid.GetRow(myButton);

RowDefinition rowDef = myGrid.RowDefinitions[rowIndex];
Community
  • 1
  • 1
Carlo
  • 25,602
  • 32
  • 128
  • 176
  • Thanks, but it doesn't do the job because I don't have a reference to myButton! – Mathias Oct 02 '09 at 21:14
  • This is exactly what I was looking for. I did have a reference to the object, I just needed to figure out which row and col it was in. Thanks, Carlo. – esteuart Oct 08 '11 at 01:08
1

The Children property of the grid object will give you a collection of all the children of the Grid (from the Panel class).

As far as getting the coordinates in the grid, look at the static methods in the Grid class (GetRow() & GetColumn()).

Hope that sets you off in the right direction.

Eric Olsson
  • 4,805
  • 32
  • 35
0

System::Windows::Controls::Grid^ myGrid = nullptr; System::Windows::Controls::UserControl^ pUserControl = nullptr;

myGrid = m_DlgOwnedObjAdmin->GrdProperties;
if (myGrid->Children->Count > 0)
{
    pUserControl = (System::Windows::Controls::UserControl^)myGrid->Children->default[0];
    if (pUserControl != nullptr)
    {
        if (bValue == true)
            pUserControl->Visibility = System::Windows::Visibility::Visible;
        else
            pUserControl->Visibility = System::Windows::Visibility::Collapsed;
    }
}
Sabi
  • 89
  • 1
  • 1
-1

you could just give your grid column/row a name

<Grid x:Name="MainGridBackground" Grid.Column="0"/>

and access it programmatically by calling it and using "."

MainGridBackground.Background = canvasUCInstance.rectanglePreview.Fill;
Wes
  • 1,847
  • 1
  • 16
  • 30
  • This doesn't seem to answer the question about getting access to a child control in the grid. – LarsTech Aug 24 '20 at 19:17
  • @LarsTech It's answering how to access the grid programmatically which was what's asked. Any controls of the grid could be accessed by the name of the grid called programmatically. – Wes Aug 25 '20 at 20:31