Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

VisualTreeHelper.GetChild Method

Returns the child visual object of a parent at the specified index.

Namespace: System.Windows.Media
Assembly: PresentationCore (in presentationcore.dll)

public static DependencyObject GetChild (
	DependencyObject reference,
	int childIndex
)
public static DependencyObject GetChild (
	DependencyObject reference, 
	int childIndex
)
public static function GetChild (
	reference : DependencyObject, 
	childIndex : int
) : DependencyObject
You cannot use methods in XAML.

Parameters

reference

The parent visual that is referenced as a DependencyObject.

childIndex

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

Return Value

The index value of the child visual object.

Use 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.

The following example shows how to enumerate all the descendants of a visual object, which is a technique you might want to use if you were interested in serializing all the rendering information of a visual object hierarchy.

// Enumerate all the descendants of the visual object.
static public 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);
    }
}

In most cases, the logical tree is a more useful representation of the elements in a WPF application. Although you do not modify the logical tree directly, this view of the application is useful for understanding property inheritance and event routing. Unlike the visual tree, the logical tree can represent non-visual data objects, such as ListItem. For more information on the logical tree, see Element Tree.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft