You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF应用中点击按钮添加控件行仅首次生效的问题

WPF添加行仅首次生效的问题修复

修正后的完整代码

XAML(若想统一布局可将第二行高度改为Auto,原结构无需修改也能正常运行)

<Window x:Class="Desing.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Desing" Height="600" Width="850"
>
    <Grid>
        <StackPanel>
            <Grid x:Name="entryGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/> <!-- 建议改为Auto,和新增行布局一致 -->
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                
                <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="SL NO"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="DESCRIPTION"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="UNIT"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="3" Content="UOM"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="4" Content="RATE"/>
                <Label HorizontalContentAlignment="Center" Grid.Column="5" Content="AMOUNT"/>
                
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="0"/>
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="1"/>
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="2"/>
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="3"/>
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="4"/>
                <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="1" Grid.Column="5"/>
            </Grid>
            
            <Grid Margin="10">
                <Button Width="50" Content="Add" Click="Button_Click"/>
            </Grid>
        </StackPanel>
    </Grid>
</Window>

C#代码

void Button_Click(object sender, RoutedEventArgs e)
{
    // 获取添加前的总行数,该数值即为新行的索引(Grid行索引从0开始)
    int newRowIndex = entryGrid.RowDefinitions.Count;
    
    // 创建符合需求的Auto高度新行
    RowDefinition newRow = new RowDefinition();
    newRow.Height = GridLength.Auto;
    entryGrid.RowDefinitions.Add(newRow);
    
    // 循环创建并添加6个TextBox
    for (int i = 0; i < 6; i++)
    {
        TextBox textBox = new TextBox();
        textBox.Margin = new Thickness(5, 2, 8, 2);
        textBox.VerticalAlignment = VerticalAlignment.Top;
        textBox.Height = 25;
        // 设置正确的行、列索引
        Grid.SetRow(textBox, newRowIndex);
        Grid.SetColumn(textBox, i);
        entryGrid.Children.Add(textBox);
    }
}

错误原因分析

  1. 行索引计算错误:原代码先添加行再获取总行数,随后用rowCount + 1设置TextBox的行索引,导致TextBox被放到不存在的行位置,后续点击时索引持续偏离实际Grid行结构,自然无法显示新行。正确逻辑是先获取添加前的总行数,该数值就是新行的索引。
  2. 行高不符合需求:原代码使用GridUnitType.Star作为新行高度,而需求明确要求Auto,这不仅会导致布局不一致,还可能因拉伸挤压其他行。
  3. 初始行高可优化:原XAML中第二行使用Height="*",改为Auto能和新增行保持统一的布局逻辑,避免不必要的拉伸。

内容的提问来源于stack exchange,提问作者Bumba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:22:02