Today I’d like to give you a short example how to enumerate all child windows in a Silverlight application:
1 2 3 4 5 6 7 8 9 10 11 12 13 | void EnumChildWindows(Visual visualParent) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualParent); i++) { Visual visual = (Visual)VisualTreeHelper.GetChild(visualParent, i); EnumChildWindows(visual); if (visual is Button) { // do something } } } |
In the code snippet above, the child windows are collected recursively. Since EnumChildWindows accepts a visualParent of Type Visual, one can start using the MainWindow object, e.g. obtained via reflection. This technique even finds child windows (buttons, text boxes, etc.) not defined in a XAML file.




