2013-07-19 39 views
0

我有几个动态创建的文本框,如果我想在面板中找到文本框什么是最好的方式来找到它?WPF C#从面板内部面板查找控件

我在网上搜索和一些通过FindName说我们可能能够找到我们的控制,但为此我需要给我的每个文本框的名称和WPF中,名称必须带有字母不是int即使我把int .ToString它会搞砸了。但是如果我把信件放在信件上,我很难通过信件找到它们,因为我可以从00开始,一路都是+1,但我不能那样做。

我有一个动态创建WrapPanel内动态创建文本框和我添加创建WrapPanel动态创建的StackPanel里面,然后我说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循环文本框txtbox):

enter image description here

+0

抱歉,但该死的家伙那是地狱混乱。我不明白在WrapPanel中的'StackPanel'中需要一个动态的'WrapPanel'。那么用'DataTemplate'和'Binding'的简单'ItemsControl'怎么样? – Viv

回答

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 
} 

正如迪克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

+0

其因为当我尝试在文本框中的文本,名称往往会以某种方式覆盖文本并在文本框中显示空白如果我把一个int.ToString的文本框的名称,但我想要的是找到最佳方式找到文本框控件。 – user2376998

+0

这些文本框是WrapPanelTest中唯一的控件吗?另外,**你有嵌套结构吗?我的意思是:WrapPanelTest中的某种面板,然后是那个里面的文本框? –

+0

你是对的 – user2376998

1

在你的循环,你是试图将WrapPanelTest.Children中的所有控件作为TextBox。 尝试:

foreach (var control in WrapPanelTest.Children) 
{ 
    if(control.GetType() == typeof(TextBox)) 
     c++; 
} 
+0

好吧,让我们这样做:内部WrapPanelTest theres Stackpanel,里面Stackpanel,theres一个wrappanel,最内层的面板是我添加文本框 – user2376998

+0

我试过你的代码,它返回0 ...它假设返回7 – user2376998

+0

它将WrappanelTest中的面板作为控件检测,因此foreach循环只进行一次。 – user2376998

1

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

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

,然后创建一个功能,如:

TextBox getTextBoxById(int id) 
{ 
    TextBox myTextBox=WrapPanelTest.FindName("textbox_"+id.ToString()) as TextBox; 
    return myTextBox; 
}