这只是一个让它工作的测试应用程序
我的主页绑定到包含创建文本框的代码的视图模型
这是我的 xaml
<StackPanel x:Name="StackSG" Grid.Row="1" Grid.Column="0"/>
<StackPanel x:Name="StackSGName" Grid.Row="1" Grid.Column="1"/>
然后我有一个按钮来实际生成文本框。这是我定义堆栈面板和创建文本框的方式。
private StackPanel stackSG;
public StackPanel StackSG
{
get { return stackSG; }
set { stackSG = value; OnNotifyPropertyChanged("StackSG"); }
}
private StackPanel stackSGName;
public StackPanel StackSGName
{
get { return stackSGName; }
set { stackSGName = value; OnNotifyPropertyChanged("StackSGName"); }
}
在这里我尝试添加文本框
private void Generate(object obj)
{
StackSG = new StackPanel();
StackSGName = new StackPanel();
int st = 10;
for (int i = 0; i < st; i++)
{
TextBox txtSG = new TextBox();
txtSG.Name = string.Format("{0}{1}", "Te", i.ToString());
txtSG.Height = 25;
txtSG.Text = string.Format("{0}{1}", "Te", i.ToString());
txtSG.IsReadOnly = true;
StackSG.Children.Add(txtSG);
//Add SG name textboxes
TextBox txtSGName = new TextBox();
txtSGName.Name = string.Format("{0}{1}", "Test", i.ToString());
txtSGName.Height = 25;
txtSGName.Text = string.Format("{0}{1}", "Test", i.ToString());
txtSGName.IsReadOnly = true;
StackSGName.Children.Add(txtSGName);
}
}
它运行没有错误,但不添加我的文本框。