我有一个网格的行和& cols动态填充。要删除行,请使用以下代码:
seivesTorGrid.RowDefinitions.RemoveAt(rowIndex+1);
一切都只能手动完成。删除行后的操作结果不令人满意:
xml中网格的定义是:
<Grid Name="seivesTorGrid" ShowGridLines="False" Margin="10, 0, 10, 0"
Width="Auto" Height="Auto" >
<Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="25"/>
</Grid.RowDefinitions>
</Grid>
这就是我添加Cols&amp; amp;划到网格:
private void AddColumns()
{
ColumnDefinition newCol = new ColumnDefinition();
//newCol.Width = GridLength.Auto;
newCol.MinWidth = 50;
newCol.MaxWidth = 150;
seivesTorGrid.ColumnDefinitions.Add(newCol);
GridViewColumnHeader gch = new GridViewColumnHeader();
gch.Content = "ID";
gch.FontSize = 12;
Grid.SetColumn(gch, 0);
seivesTorGrid.Children.Add(gch);
for (int i = 0; i < GlobalUtils.TOR_List.Count; i++)
{
newCol = new ColumnDefinition();
newCol.Width = GridLength.Auto;
newCol.MinWidth = 80;
newCol.MaxWidth = 150;
seivesTorGrid.ColumnDefinitions.Add(newCol);
gch = new GridViewColumnHeader();
gch.Content = GlobalUtils.TOR_List[i].TOR_Id;
gch.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
gch.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
gch.FontSize = 12;
Grid.SetColumn(gch, i + 1);
seivesTorGrid.Children.Add(gch);
}
}
private void AddRowToGrid(String id)
{
RowDefinition newRow = new RowDefinition();
seivesTorGrid.RowDefinitions.Add(newRow);
int rowIndex = seivesTorGrid.RowDefinitions.Count - 1;
TextBlock idTB = new TextBlock();
idTB.Text = id;
idTB.TextAlignment = TextAlignment.Left;
idTB.VerticalAlignment = System.Windows.VerticalAlignment.Center;
Grid.SetColumn(idTB, 0);
Grid.SetRow(idTB, rowIndex);
seivesTorGrid.Children.Add(idTB);
for (int i = 1; i < 3; i++)
{
Button tempBtn = new Button() {
Height=25,
Width=80,
Margin = new Thickness(5),
Content = "Add",
Background = Brushes.Brown,
Tag = rowIndex
};
tempBtn.Click += OnIdButtonClick;
Grid.SetColumn(tempBtn, i);
Grid.SetRow(tempBtn, rowIndex);
seivesTorGrid.Children.Add(tempBtn);
}
}
为什么删除行后的结果不正确?需要纠正的是什么?
非常感谢任何帮助。
答案 0 :(得分:1)
您当然还必须删除该行中的所有孩子。
这意味着seivesTorGrid.Children
集合中的所有元素,其中Grid.GetRow()
返回要删除的索引。