简体   繁体   English

WPF:如何删除动态创建的文本框

[英]WPF: How to remove textbox created dynamically

I am creating multiple textbox, I was wondering since I created these textbox dynamically how would I remove them or even update them? 我正在创建多个文本框,我想知道自从动态创建这些文本框以来,如何删除它们甚至更新它们?

For example in code 例如在代码中

TextBox tb = new TextBox();
tb.Name = "Temp"

How would I remove or update textbox Temp 我如何删除或更新文本框Temp

I cannot called Temp.Text = "Test" 我不能叫Temp.Text = "Test"

Also I do not have access to tb.Text. 另外,我无权访问tb.Text。 I have created a bunch of textboxes and named them by a number textbox_1 . 我创建了一堆文本框,并用数字textbox_1命名它们。 When a certain button is clicked I want to update the textbox that is clicked. 单击某个按钮后,我要更新所单击的文本框。

You can find that textbox from the container using Name like: 您可以使用“ Name从容器中找到该文本框,例如:

TextBox tb = (TextBox) this.someStackPanel.FindName("Temp");

then later 然后再

tb.Text = "Test";

To remove it you can do: 要删除它,您可以执行以下操作:

//First find the TextBox
TextBox tb = (TextBox) this.someStackPanel.FindName("Temp");
//Then remove it 
this.someStackPanel.Children.Remove(tb);

You need to call 你需要打电话

parent.RegisterName("Temp", tb);

on the parent after creating the TextBox to register the name. 创建文本框以注册名称后,在父项上单击。

When you want to find the TextBox, call 当您要查找文本框时,请调用

TextBox tb = (TextBox) parent.FindName("Temp")

to get the TextBox. 获取文本框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM