这听起来像你需要一个递归常规像下面GetTextBoxes:
void Page_Loaded(object sender, RoutedEventArgs e)
{
// Instantiate a list of TextBoxes
List<TextBox> textBoxList = new List<TextBox>();
// Call GetTextBoxes function, passing in the root element,
// and the empty list of textboxes (LayoutRoot in this example)
GetTextBoxes(this.LayoutRoot, textBoxList);
// Now textBoxList contains a list of all the text boxes on your page.
// Find all the non empty textboxes, and put them into a list.
var nonEmptyTextBoxList = textBoxList.Where(txt => txt.Text != string.Empty).ToList();
// Do something with each non empty textbox.
nonEmptyTextBoxList.ForEach(txt => Debug.WriteLine(txt.Text));
}
private void GetTextBoxes(UIElement uiElement, List<TextBox> textBoxList)
{
TextBox textBox = uiElement as TextBox;
if (textBox != null)
{
// If the UIElement is a Textbox, add it to the list.
textBoxList.Add(textBox);
}
else
{
Panel panel = uiElement as Panel;
if (panel != null)
{
// If the UIElement is a panel, then loop through it's children
foreach (UIElement child in panel.Children)
{
GetTextBoxes(child, textBoxList);
}
}
}
}
实例化的文本框的空列表。调用GetTextBoxes,传递页面上的根控件(在我的情况下,这就是this.LayoutRoot),并且GetTextBoxes应该递归循环遍历每个UI控件的UI元素,测试它是否是TextBox(添加它列表),或者一个小组,可能有它自己的后代来递归。
希望有所帮助。 :)
非常感谢! 这绝对是我一直在寻找的。现在我只需要看看我能否通过UIElement课程获取更多信息。 (即Control x:Name如果可能的话) – coson 2009-03-03 00:23:11
我不认为你可以将`ScrollViewer`作为`Panel`来投射,所以如果你可能需要得到一个的孩子,你需要添加另一个`else` 。 – mbomb007 2015-06-17 16:35:05