如何使用c#在WPF中在运行时动态添加文本框控件

时间:2016-09-06 05:31:06

标签: .net wpf

我是WPF的新手。我想在运行时为每一行创建3个文本框,当我点击生成按钮时。请帮帮我。

Automatically created textbox

 **Code behind** 
  private List<TextBox> inputTextBoxes;
    private void btnGenerate_Click(object sender, RoutedEventArgs e)
    {
        //Get the number of input text boxes to generate
        int inputNumber = Int32.Parse(textBoxInput.Text);

        //Initialize list of input text boxes
        inputTextBoxes = new List<TextBox>();

        //Generate labels and text boxes
        for (int i = 1; i <= inputNumber; i++)
        {
            //Create a new label and text box
            Label labelInput = new Label();
            TextBox textBoxNewInput = new TextBox();
        }
    }

2 个答案:

答案 0 :(得分:0)

我认为您网页的内容是Canvas控件:

代码背后:

protected void ButtonGenerate_Click(object sender, RoutedEventArgs e)
{
    TextBox tb = new TextBox();
    (this.Content as Canvas).Children.Add(tb);
}

您可以在https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox(v=vs.110).aspx

中找到其他文本框属性

答案 1 :(得分:0)

只需在xaml中为您的网格命名:

<Grid x:Name="Grid1">
    <Button Grid.Column="0"
            Margin="10"
            Click="btnGenerate_Click">
        Button Content
    </Button>
</Grid>

并在代码背后:

private void btnGenerate_Click(object sender, RoutedEventArgs e)
{
    //Get the number of input text boxes to generate
    int inputNumber = Int32.Parse(textBoxInput.Text);

    //Initialize list of input text boxes
    inputTextBoxes = new List<TextBox>();

    //Generate labels and text boxes
    for (int i = 1; i <= inputNumber; i++)
    {
        //Create a new label and text box
        Label labelInput = new Label();
        Grid1.Children.Add(labelInput);
        TextBox textBoxNewInput = new TextBox();
        Grid1.Children.Add(textBoxNewInput);
    }
}

如果需要,您也可以设置位置,以便新创建的元素不会相互重叠。

编辑:

您需要创建包含所需行和列的网格,然后将每个文本框或标签放在所需的行列组合中。 它包含6行3列。请参阅以下示例:

<Grid Name="Grid1">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
</Grid> 

private void btnGenerate_Click(object sender, RoutedEventArgs e)
{
    //Get the number of input text boxes to generate
    int inputNumber = Int32.Parse(textBoxInput.Text);

    //Initialize list of input text boxes
    inputTextBoxes = new List<TextBox>();

    //Generate labels and text boxes
    for (int i = 1; i <= inputNumber; i++)
    {
        //Create a new label and text box
        Label labelInput = new Label();

        Grid.SetColumn(labelInput, i);
        Grid.SetRow(labelInput, i);
        Grid1.Children.Add(labelInput);

        TextBox textBoxNewInput = new TextBox();
        Grid.SetColumn(labelInput, i+1);
        Grid.SetRow(labelInput, i);
        Grid1.Children.Add(textBoxNewInput);
    }
}