我如何在WPF中动态地捕获编号的文本框的文本?

发布于 2025-02-06 03:22:13 字数 761 浏览 3 评论 0原文

我正在创建一个联系人添加,我想添加选项以添加超过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2025-02-13 03:22:13

使用foreach循环:

foreach(TextBox tb in PhoneNumberStackPanel.Children.OfType<TextBox>())
{
    string text = tb.Text;
}

还考虑用itemscontrol替换stackpanel :

private void AddPhoneNumberButton_Click(object sender, RoutedEventArgs e)
{
    int numberOfTextBoxes = PhoneNumberItemsControl.Items.Count;

    TextBox txtbox = new TextBox();
    txtbox.Name = $"PhoneNumberTextBox{numberOfTextBoxes}";
    txtbox.Width = 200;
    txtbox.Background = Brushes.White;
    txtbox.Margin = new Thickness(0,10,0,0);
    PhoneNumberItemsControl.Items.Add(txtbox);
}

foreach(TextBox tb in PhoneNumberItemsControl.Items.OfType<TextBox>())
{
}

您可以进行的下一步是将文本框移动到itemscontrol.itemtemplate。 (一旦您需要比单个元素更复杂的视觉结构,它就会成为“必须做的”)

use foreach loop:

foreach(TextBox tb in PhoneNumberStackPanel.Children.OfType<TextBox>())
{
    string text = tb.Text;
}

also consider replacing StackPanel with ItemsControl:

private void AddPhoneNumberButton_Click(object sender, RoutedEventArgs e)
{
    int numberOfTextBoxes = PhoneNumberItemsControl.Items.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);
    PhoneNumberItemsControl.Items.Add(txtbox);
}

foreach(TextBox tb in PhoneNumberItemsControl.Items.OfType<TextBox>())
{
}

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文