0

我有一些动态创建的文本框,如果我想在面板中找到文本框,找到它的最佳方法是什么?

我在网上搜索,有人说通过 FindName 我们可能能够找到我们的控件,但为此我需要为我的每个文本框命名,并且在 WPF 中,即使我输入 int.ToString ,名称也必须带有非 int 字母搞砸了。但是如果我输入字母,我很难通过字母找到它们,数字会很好,因为我可以一直从 00 和 +1 开始,但我不能那样做。

我在动态创建的 WrapPanel 中动态创建了文本框,我在动态创建的 stackPanel 中添加了动态创建的 WrapPanel,然后我将该 stackkpanel 添加到我在 xaml 端创建的 WrapPanel

如果你问我为什么需要这么多面板,因为这是我可以让我看起来更好的唯一方法,因为我从 db 检索信息并显示它的方式。

这是我的代码的样子(我把它缩短了,因为它太长了):

       private void PopulateQuestion(int activityID, int taskID)
    {
        IList<Model.questionhint> lstQuestionHints = qh.GetRecords(taskID, activityID); 

        StackPanel sp = new StackPanel();

        foreach (Model.questionhint qhm in lstQuestionHints)

        {
            WrapPanel wp = new WrapPanel();

           //some code ....

            if (qhm.Option1.Trim().Length > 0 && 
               qhm.Option2.Trim().Length > 0)
            {
                wp.Children.Add(space);
                wp.Children.Add(tbox); //

            }
               sp.Children.Add(wp);// Adding wrap panel to stackpanel
            } // end of for each loop.

            WrapPanelTest.Children.Add(sp); // Adding stackpanel to WrapPanel ( in xaml)

        }

WrapPanelTest 是我在 xaml 端创建的面板。所以现在如果我有一个按钮,我应该如何从这些面板中找到文本框控件?

我试过了 :

     private void button1_Click(object sender, RoutedEventArgs e) // Check Button
    {
            int c = 0;

        foreach (TextBox txtbox in WrapPanelTest.Children)
        {
            c++;
        } 

      MessageBox.Show(c);

}

但它显示了这个错误(指向 foreach 循环中的 TextBox txtbox):

在此处输入图像描述

4

3 回答 3

2

在您的循环中,您试图将 WrapPanelTest.Children 中的所有控件作为文本框。尝试:

foreach (var control in WrapPanelTest.Children)
{
    if(control.GetType() == typeof(TextBox))
        c++;
} 
于 2013-07-19T12:02:12.867 回答
1

您应该为这些文本框创建一个命名约定。例如:

int id=1;
tbox.Name="textbox_"+id.ToString();

然后创建一个函数,如:

TextBox getTextBoxById(int id)
{
   TextBox myTextBox=WrapPanelTest.FindName("textbox_"+id.ToString()) as TextBox;
   return myTextBox;
}
于 2013-07-19T12:10:39.657 回答
1

我不明白你用字母和数字命名控件的问题。像这样做:

 // This is the place where you dynamically create the textboxes, I skipped the part where u add it to wrap panel etc.
for( int numControls = 0; numControls < 30;  numControls++)
{
  Textbox box = new Texbox();
  box.name = "textbox" +     numControls.ToString();
}

然后你发现它只是使用

for(int numBoxes = 0;numBoxes < 30; numBoxes++)
{
Textbox box = WrapPanelTest.FindNyName("textbox" + numBoxes.ToString();
//operate on these
}

正如 Dick Schuerman 的解决方案:首先,帮助我们更容易找到孩子的辅助类:

class ChildControls
{
    private List<object> lstChildren;

    public List<object> GetChildren(Visual p_vParent, int p_nLevel)
    {
        if (p_vParent == null)
        {
            throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString());
        }

        this.lstChildren = new List<object>();

        this.GetChildControls(p_vParent, p_nLevel);

        return this.lstChildren;

    }

    private void GetChildControls(Visual p_vParent, int p_nLevel)
    {
        int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);

        for (int i = 0; i <= nChildCount - 1; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);

            lstChildren.Add((object)v);

            if (VisualTreeHelper.GetChildrenCount(v) > 0)
            {
                GetChildControls(v, p_nLevel + 1);
            }
        }
    }
}

你像这样使用它:

            ChildControls ccChildren = new ChildControls();

        foreach (object o in ccChildren.GetChildren(WrapPanelTest, 5))
        {
            if (o.GetType() == typeof(TextBox))
            {
                // Do something
            }
        }

GetChildren 中的“5”表示您要挖掘的深度。例子:

  • WrapPanelTest
    • 网格
    • 文本框

那会希望您将该属性设置为 3。

于 2013-07-19T15:38:23.803 回答