如何使用 C# 在 WPF 应用程序上单击按钮来添加控件行?

问题描述 投票:0回答:1

我有一个非常简单的要求。我有一个 WPF 应用程序,当前有 2 个网格。第一个网格应该包含某种数据输入形式,第二个网格只有一个“添加”按钮。

<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="*"/>
                </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>

我想要的是,当我单击“添加”按钮时,应将具有相同属性的文本框的新行添加到视图中。单击按钮后,它将添加到 xaml 中另一个

<RowDefinition Height="Auto"/>

        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="0"/>
        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="1"/>
        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="2"/>
        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="3"/>
        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="4"/>
        <TextBox Margin="5 2 8 2" VerticalAlignment="Top" Height="25" Grid.Row="2" Grid.Column="5"/>

视图将变成

来自

等等... 最好的方法是什么?

我尝试过以下代码:

void Button_Click(object sender, RoutedEventArgs e)
{
    RowDefinition newRow = new RowDefinition();
    newRow.Height = new GridLength(1, GridUnitType.Star);
    entryGrid.RowDefinitions.Add(newRow);
    
    int rowCount = entryGrid.RowDefinitions.Count;
    
    TextBox[] textBoxes = new TextBox[6];

    for (int i = 0; i < textBoxes.Length; i++)
    {
        textBoxes[i] = new TextBox();
        textBoxes[i].Margin = new Thickness(5, 2, 8, 2);
        textBoxes[i].VerticalAlignment = VerticalAlignment.Top;
        textBoxes[i].Height = 25;
        Grid.SetRow(textBoxes[i], rowCount + 1);
        Grid.SetColumn(textBoxes[i], i);
        entryGrid.Children.Add(textBoxes[i]);
    }
    
    MessageBox.Show("New row added!");
}

但它仅在我第一次单击按钮时有效,后续单击时不会添加新行。

c# wpf
1个回答
0
投票

您不需要

TextBox[]

此外,由于

RowDefinitions
的索引是从零开始的,因此您应该将
Grid.Row
附加属性设置为
entryGrid.RowDefinitions.Count - 1

试试这个:

void Button_Click(object sender, RoutedEventArgs e)
{
    RowDefinition newRow = new RowDefinition();
    newRow.Height = new GridLength(1, GridUnitType.Star);
    entryGrid.RowDefinitions.Add(newRow);

    int rowCount = entryGrid.RowDefinitions.Count - 1;

    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, rowCount);
        Grid.SetColumn(textBox, i);
        entryGrid.Children.Add(textBox);
    }

    MessageBox.Show("New row added!");
}

正如评论中提到的,您可能想了解如何使用

DataGrid
控件或
ItemsControl
来重复项目。

© www.soinside.com 2019 - 2024. All rights reserved.