0

我有一个网格,其行和列是动态填充的。要删除一行,我使用以下代码:

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 & Row 添加到 Grid 的方式:

        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);
        }                       
    }

为什么删除行后的结果不正确?需要纠正什么?

非常感谢任何帮助。

4

1 回答 1

1

当然,您还必须删除该行中的所有子项。

这意味着seivesTorGrid.Children集合中的所有元素,其中Grid.GetRow()返回要删除的索引。

于 2012-05-24T12:14:03.000 回答