当我编写 Controls.Add() 以在窗口中动态创建控件时,它表示控件未定义或存在于窗口类中。换句话说,“控件”没有被识别为类或命令,因此代码不起作用。任何建议将不胜感激。
我想动态创建一个窗口并向其添加一个文本框。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Window f1 = new Window();
f1.Show();
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 60;
tb.Name = "TextBoxID";
tb.Text = "This is textbox first data";
Controls.Add(f1);
}
}