ContainerVisual.VisualChildrenCount Property


Gets the number of children for the ContainerVisual.

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

protected override sealed int VisualChildrenCount { get; }

Property Value

Type: System.Int32

The number of children in the VisualCollection of the ContainerVisual.

By default, a ContainerVisual does not have any children.

Notes to Inheritors:

Classes that derive from ContainerVisual must implement the VisualChildrenCount property to enumerate the visual children. The derived property must return the number of children for the ContainerVisual.

The visual tree cannot be modified during this call.

The following example shows how to create a ContainerVisual object that is used as the parent for two DrawingVisual objects. Objects that are added to the ContainerVisual object must be added in reverse z-order (bottom to top) to ensure they are rendering in the correct drawing order. In order for the visual tree to be enumerated correctly, the example provides overridden implementations of the GetVisualChild method and VisualChildrenCount property.

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual objects.
public class MyContainerVisualHost : FrameworkElement
    private ContainerVisual _containerVisual;

    public MyContainerVisualHost(DrawingVisual border, DrawingVisual text)
        // Create a ContainerVisual to hold DrawingVisual children.
        _containerVisual = new ContainerVisual();

        // Add children to ContainerVisual in reverse z-order (bottom to top).

        // Create parent-child relationship with host visual and ContainerVisual.

    // Provide a required override for the VisualChildrenCount property.
    protected override int VisualChildrenCount
        get { return _containerVisual == null ? 0 : 1; }

    // Provide a required override for the GetVisualChild method.
    protected override Visual GetVisualChild(int index)
        if (_containerVisual == null)
            throw new ArgumentOutOfRangeException();

        return _containerVisual;

.NET Framework
Available since 3.0
Return to top