Parece que necesita un recursiva de rutina, como GetTextBoxes siguientes:
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);
}
}
}
}
Instantiate una lista vacía de cuadros de texto. Llamar a GetTextBoxes, pasando el control raíz en su página (en mi caso, eso es esto. LayoutRoot), y GetTextBoxes deberían recorrer recursivamente cada elemento de UI que es un descendiente de ese control, probando para ver si es un TextBox (agréguelo) a la lista), o un panel, que podría tener descendientes propios para recurrir a través de.
Espero que ayude. :)
¡Muchas gracias! Esto es definitivamente lo que estaba buscando. Ahora solo necesito ver si puedo usar la clase UIElement para extraer más información. (es decir, Control x: Nombre si es posible) – coson
No creo que pueda convertir un 'ScrollViewer' como un' Panel', por lo que si necesita obtener el elemento secundario de uno, deberá agregar otro 'else' . – mbomb007