我如何在WPF中动态地捕获编号的文本框的文本?
我正在创建一个联系人添加,我想添加选项以添加超过1个电话号码。 我允许创建更多文本框,以便用户使用此按钮添加数字:
private void AddPhoneNumberButton_Click(object sender, RoutedEventArgs e)
{
int numberOfTextBoxes = PhoneNumberStackPanel.Children.OfType<TextBox>().Count();
TextBox txtbox = new TextBox();
txtbox.Name = $"PhoneNumberTextBox{numberOfTextBoxes}";
txtbox.Width = 200;
txtbox.Background = Brushes.White;
txtbox.Margin = new Thickness(0,10,0,0);
PhoneNumberStackPanel.Children.Add(txtbox);
}
现在如何动态地参考这些文本框。就像我想使用此循环中在这些文本框中捕获所有文本:
for ( int i = 0 ; i < PhoneNumberStackPanel.Children.OfType<TextBox>().Count() ; i++){}
I am creating a contacts add and I want to add the option to add more than 1 phone numbers.
I allow the creation of more textboxes for the user to add number with this button:
private void AddPhoneNumberButton_Click(object sender, RoutedEventArgs e)
{
int numberOfTextBoxes = PhoneNumberStackPanel.Children.OfType<TextBox>().Count();
TextBox txtbox = new TextBox();
txtbox.Name = quot;PhoneNumberTextBox{numberOfTextBoxes}";
txtbox.Width = 200;
txtbox.Background = Brushes.White;
txtbox.Margin = new Thickness(0,10,0,0);
PhoneNumberStackPanel.Children.Add(txtbox);
}
Now how to I refer to those textboxes dynamically. Like if I want to grab all the text in these textboxes with this loop:
for ( int i = 0 ; i < PhoneNumberStackPanel.Children.OfType<TextBox>().Count() ; i++){}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
foreach
循环:还考虑用
itemscontrol
替换stackpanel :您可以进行的下一步是将文本框移动到
itemscontrol.itemtemplate
。 (一旦您需要比单个元素更复杂的视觉结构,它就会成为“必须做的”)use
foreach
loop:also consider replacing StackPanel with
ItemsControl
:next step which you can make is moving TextBoxes into
ItemsControl.ItemTemplate
. (it becomes a "must-do" as soon as you need visual structure more complex than a single element)