2016-01-06 52 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

相關問題