使用 FindName 后如何更改动态创建的文本框内容?

How to change dynamic created textbox Content after using FindName?

我在 WrapPanel 中创建了一个动态 TextBox,我想在按钮单击事件中对其进行更改。

我正在使用 FindName 找到控件,但是这之后我应该做什么?或者,是否有使用 Name?

查找控件的不同方法

这是我的代码:

WrapPanel wpOrderList = new WrapPanel();
TextBox txtCount = new TextBox();
txtCount.Text = "1";
txtCount.Height = 20;
txtCount.Width = 20;
txtCount.Name = "txtCount_" + global;
wpOrderList.Children.Add(txtCount);

void btnPlus_Click(object sender, RoutedEventArgs e)
{
    try
    {
        string[] strngname = (sender as Button).Name.ToString().Split('_');
        this.FindName("txtCount_" + strngname[1]);
        //What should I do to change the textbox text now here?
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
    }
}
var textbox = this.FindName("txtCount_" + strngname[1]) as TextBox;

if (textbox != null)
   textbox.Text = "Test";

在xaml

         <StackPanel x:Name="stk">
            <Button Click="Button_Click_1"  Height="50">Click</Button>
        </StackPanel>

在 Windows 加载的事件中

            WrapPanel wpOrderList = new WrapPanel();
            TextBox txtCount = new TextBox();
            txtCount.Text = "1";
            txtCount.Height = 20;
            txtCount.Width = 20;
            txtCount.Name = "txt";
            wpOrderList.Children.Add(txtCount);
            stk.Children.Add(wpOrderList);

点击按钮

           TextBox foundTextBox = FindChild<TextBox>(this, "txt");

            foundTextBox.Text = "fdf";

辅助函数

public static T FindChild<T>(DependencyObject parent, string childName)
           where T : DependencyObject
        {
            // Confirm parent and childName are valid. 
            if (parent == null) return null;

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child. 
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }

后面代码中的完整代码

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            TextBox foundTextBox = FindChild<TextBox>(this, "txt");

            foundTextBox.Text = "fdf";
        }

        public static T FindChild<T>(DependencyObject parent, string childName)
           where T : DependencyObject
        {
            // Confirm parent and childName are valid. 
            if (parent == null) return null;

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(parent, i);
                // If the child is not of the request child type child
                T childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child. 
                    if (foundChild != null) break;
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WrapPanel wpOrderList = new WrapPanel();
            TextBox txtCount = new TextBox();
            txtCount.Text = "1";
            txtCount.Height = 20;
            txtCount.Width = 20;
            txtCount.Name = "txt";
            wpOrderList.Children.Add(txtCount);
            stk.Children.Add(wpOrderList);
        }    
    }