我想删除网格的最后一行。我希望最后一行删除其中包含“2”的行。但无论我删除第 0 行还是第 1 行,第 0 行总是被删除。我怀疑我遗漏了一些关于行与网格的微妙之处。我究竟做错了什么:
// 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);
注意:我也试过:
myGrid.RowDefinitions.Remove(myGrid.RowDefinitions[1]);
同样的结果。感谢您的任何见解。