VisualTreeHelper.GetChild(DependencyObject, Int32) Method

Definition

Returns the child visual object from the specified collection index within a specified parent.

public static System.Windows.DependencyObject GetChild (System.Windows.DependencyObject reference, int childIndex);

Parameters

reference
DependencyObject

The parent visual, referenced as a DependencyObject.

childIndex
Int32

The index that represents the child visual that is contained by reference.

Returns

The index value of the child visual object.

Examples

The following example shows how to enumerate all the descendants of a visual object. This is a technique you might use if you are interested in serializing all the rendering information of a visual object hierarchy or are performing analysis or alternative rendering.

// Enumerate all the descendants of the visual object.
public static void EnumVisual(Visual myVisual)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
    {
        // Retrieve child visual at specified index value.
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        // Do processing of the child visual object.

        // Enumerate children of the child visual object.
        EnumVisual(childVisual);
    }
}

Remarks

Call the GetChildrenCount method to determine the total number of child elements of a parent visual.

The value of reference can represent either a Visual or Visual3D object, which is why the common base type DependencyObject is used here as a parameter type.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9