#112 – Navigating the Visual Tree in Code

You can use the VisualTreeHelper.GetChildrenCount and GetChild methods to enumerate all objects in a visual tree.

To get all of the children in a visual tree of a specified parent object, you can use a for loop:

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
                Debug.WriteLine(VisualTreeHelper.GetChild(obj, i));

You could extend this example and descend down the visual tree by calling GetChild methods on the children returned from the first call.

Advertisements