如何使用C#在WPF中的运行时中动态添加文本框控件 [英] How to add textbox control dynamically in runtime in WPF using c#

查看:42
本文介绍了如何使用C#在WPF中的运行时中动态添加文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in WPF.I want to create 3 textbox for each row at runtime when i click generate button. please help me.

自动创建的文本框

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

推荐答案

只需在xaml中为网格命名:

Just give your grid a name in xaml :

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

以及位于其后的代码中:

and in code behind :

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

如果需要,还可以设置位置,以使新创建的元素不会相互重叠.

If you want, you can set the position also so that the newly created elements doesn't overlap each other.

您需要使用所需的行和列创建一个网格,然后将每个文本框或标签放置在所需的行-列组合内.它包含6行3列.请参见下面的示例:

You need to create a grid with required rows and columns and then place each textbox or label inside the required row-column combination. It contains 6 rows and 3 columns. Please see the example below :

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

这篇关于如何使用C#在WPF中的运行时中动态添加文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆