2016-01-06 79 views
0

我需要使用代码隐藏在另一个textbox之后立即创建XAML textbox。也许是这样的:在StackPanel中的特定元素之后创建元素

之前

<StackPanel> 
    <TextBox Name="TextBox1"/> 
    <TextBox Name="TextBox2"/> 
    <TextBox Name="TextBox3"/> 
</StackPanel> 

<StackPanel> 
    <TextBox Name="TextBox1"/> 
    <TextBox Name="TextBox2"/> 
    <TextBox Name="InsertedTextBox"/> <!--This textbox was inserted after 'TextBox2'--> 
    <TextBox Name="TextBox3"/> 
</StackPanel> 

后,我有textbox我希望之后插入其他textbox的名称。我是否可以知道如何在textbox之后插入textbox这是我知道的名字?

注:我是通用Windows编程。

在此先感谢。

回答

1

您需要命名StackPanel引用它的代码,你需要在前TextBox的指标:

var index = this.stackPanel1.Children.IndexOf(TextBox2); 
this.stackPanel1.Children.Insert(index + 1, new TextBox { Name = "InsertedTextBox" }); 
0

你可以试试这个,注意我给了StackPanel一个名字。

// Where 2 is the index. 
this.StackPanel1.Children.Insert(2, new TextBox()); 
+0

非常抱歉 - 这两个答案的工作,但我只能标记一个答案回答。 – Theo

相关问题