這聽起來像你需要一個遞歸常規像下面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