This documentation is archived and is not being maintained.

VisualTreeHelper.GetChildrenCount Method

Returns the number of children that a parent visual contains.

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

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



The parent visual that is referenced as a DependencyObject.

Return Value

The number of child visuals that the parent visual contains.

Use the GetChild method to retrieve a specified child 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.

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