如何在按钮单击上动态打开文本框

时间:2014-01-24 05:53:49

标签: c# wpf

我想在此按钮上单击打开文本框(在用户单击按钮时在运行时创建),并存储字符串。

private void button5_Click(object sender, RoutedEventArgs e)
{
    TextBox dynamicTextBox = new TextBox();
    dynamicTextBox.Text = "Type Partnumber";
} 

3 个答案:

答案 0 :(得分:1)

将TextBox添加到Grid子项并设置列号和行号。

private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextBox dynamicTextBox = new TextBox();
            dynamicTextBox.Text = "Type Partnumber";
            Grid.SetRow(dynamicTextBox, 1);
            Grid.SetColumn(dynamicTextBox, 0);
            this.MainGrid.Children.Add(dynamicTextBox);
        }

答案 1 :(得分:0)

试试这个

private void button5_Click(object sender, EventArgs e)
        {
            TextBox dynamicTextBox = new TextBox();
            // your code
            this.Controls.Add(dynamicTextBox);
        }

答案 2 :(得分:0)

说,您的Grid部分中有一个XAML,并将其命名为“网格”并访问该网格以添加新创建的TextBox

private void button5_Click(object sender, RoutedEventArgs e)
{
  TextBox dynamicTextBox = new TextBox();
  dynamicTextBox.Text = "Type Partnumber";
  grid.Children.Add(dynamicTextBox);
}