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

时间:2015-12-24 11:32:27

标签: c# wpf textbox wpf-controls buttonclick

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

我使用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();
    }
}

2 个答案:

答案 0 :(得分:1)

在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);
        }    
    }   

答案 1 :(得分:0)

var textbox = this.FindName("txtCount_" + strngname[1]) as TextBox;

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