ContainerVisual.VisualChildrenCount Propriété

Définition

Obtient le nombre d’enfants pour le ContainerVisual.Gets the number of children for the ContainerVisual.

protected:
 property int VisualChildrenCount { int get(); };
protected override sealed int VisualChildrenCount { get; }
member this.VisualChildrenCount : int
Protected Overrides NotOverridable ReadOnly Property VisualChildrenCount As Integer

Valeur de propriété

Int32

Nombre d’enfants dans la VisualCollection du ContainerVisual.The number of children in the VisualCollection of the ContainerVisual.

Exemples

L’exemple suivant montre comment créer un ContainerVisual objet qui est utilisé comme parent pour deux DrawingVisual objets.The following example shows how to create a ContainerVisual object that is used as the parent for two DrawingVisual objects. Les objets ajoutés à l' ContainerVisual objet doivent être ajoutés dans l’ordre de plan inverse (de bas en haut) pour s’assurer qu’ils sont rendus dans l’ordre de dessin correct.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. Pour que l’arborescence d’éléments visuels soit correctement énumérée, l’exemple fournit des implémentations substituées de la GetVisualChild méthode et de la VisualChildrenCount propriété.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).
        _containerVisual.Children.Add(border);
        _containerVisual.Children.Add(text);

        // Create parent-child relationship with host visual and ContainerVisual.
        this.AddVisualChild(_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;
    }
}
' 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
    Inherits FrameworkElement
    Private _containerVisual As ContainerVisual

    Public Sub New(ByVal border As DrawingVisual, ByVal text As DrawingVisual)
        ' Create a ContainerVisual to hold DrawingVisual children.
        _containerVisual = New ContainerVisual()

        ' Add children to ContainerVisual in reverse z-order (bottom to top).
        _containerVisual.Children.Add(border)
        _containerVisual.Children.Add(text)

        ' Create parent-child relationship with host visual and ContainerVisual.
        Me.AddVisualChild(_containerVisual)
    End Sub

    ' Provide a required override for the VisualChildrenCount property.
    Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
        Get
            Return If(_containerVisual Is Nothing, 0, 1)
        End Get
    End Property

    ' Provide a required override for the GetVisualChild method.
    Protected Overrides Function GetVisualChild(ByVal index As Integer) As Visual
        If _containerVisual Is Nothing Then
            Throw New ArgumentOutOfRangeException()
        End If

        Return _containerVisual
    End Function
End Class

Remarques

Par défaut, un ContainerVisual n’a pas d’enfants.By default, a ContainerVisual does not have any children.

Notes pour les héritiers

Les classes qui dérivent de ContainerVisual doivent implémenter la VisualChildrenCount propriété pour énumérer les enfants visuels.Classes that derive from ContainerVisual must implement the VisualChildrenCount property to enumerate the visual children. La propriété dérivée doit retourner le nombre d’enfants pour le ContainerVisual .The derived property must return the number of children for the ContainerVisual.

Impossible de modifier l’arborescence d’éléments visuels pendant cet appel.The visual tree cannot be modified during this call.

S’applique à

Voir aussi