編輯

共用方式為


VisualTreeHelper.GetChildrenCount(DependencyObject) Method

Definition

Returns the number of children that the specified visual object contains.

public:
 static int GetChildrenCount(System::Windows::DependencyObject ^ reference);
public static int GetChildrenCount (System.Windows.DependencyObject reference);
static member GetChildrenCount : System.Windows.DependencyObject -> int
Public Shared Function GetChildrenCount (reference As DependencyObject) As Integer

Parameters

reference
DependencyObject

The parent visual that is referenced as a DependencyObject.

Returns

The number of child visuals that the parent visual contains.

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);
    }
}
' 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.
        EnumVisual(childVisual)
    Next i
End Sub

Remarks

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, which is why the common base type DependencyObject is used here as a parameter type.

Applies to