VisualTreeHelper.GetChild Method (DependencyObject, Int32)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Shared Function GetChild (
	reference As DependencyObject,
	childIndex As Integer
) As DependencyObject


Type: System.Windows.DependencyObject

The parent visual, referenced as a DependencyObject.

Type: System.Int32

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

Return Value

Type: System.Windows.DependencyObject

The index value of the child visual object.

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.

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 Shared Sub EnumVisual(ByVal myVisual As Visual)
	For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(myVisual) - 1
		' Retrieve child visual at specified index value.
		Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(myVisual, i), Visual)

		' Do processing of the child visual object.

		' Enumerate children of the child visual object.
	Next i
End Sub

.NET Framework
Available since 3.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top