I want to remove the last row of a grid. I expect the last line to remove the row with the '2' in it. But whether I removeAt row 0 or 1, the 0th row is always removed. I suspect I am missing something subtle about rows vs grids. What am I doing wrong:
// Create row 0
RowDefinition row = new RowDefinition();
row.Height = GridLength.Auto;
myGrid.RowDefinitions.Add(row);
// Add someting to it
TextBox tb = new TextBox();
tb.Text = "1";
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, 0);
myGrid.Children.Add(tb);
// create row 1
row = new RowDefinition();
row.Height = GridLength.Auto;
myGrid.RowDefinitions.Add(row);
// Add something to it
tb = new TextBox();
tb.Text = "2";
Grid.SetColumn(tb, 0);
Grid.SetRow(tb, 1);
myGrid.Children.Add(tb);
// Delete row 1 (second row 0-indexed) <<< only row 0 is deleted
myGrid.RowDefinitions.RemoveAt(1);
Note: I also tried:
myGrid.RowDefinitions.Remove(myGrid.RowDefinitions[1]);
same result. Thanks for any insight.
When you delete a row from a Grid it doesn't remove the children on that row, you will have to do that yourself. You could try iterating over the children of the Grid and remove any elements with a Row value equal to the row you want to remove...
var rowToDelete = 3;
foreach (var element in myGrid.Children)
{
if(Grid.GetRow(element) == rowToDelete)
{
myGrid.Children.Remove(element);
}
}