ContainerVisual.VisualChildrenCount Свойство

Определение

Возвращает количество дочерних элементов для 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

Значение свойства

Количество дочерних элементов в VisualCollection для ContainerVisual.

Примеры

В следующем примере показано, как создать ContainerVisual объект , который используется в качестве родительского объекта для двух DrawingVisual объектов. Объекты, добавленные в ContainerVisual объект, должны быть добавлены в обратном порядке Z (снизу вверх), чтобы обеспечить отрисовку в правильном порядке рисования. Для правильного перечисления визуального дерева в примере приводятся переопределенные реализации GetVisualChild метода и VisualChildrenCount свойства .

// 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

Комментарии

По умолчанию ContainerVisual у объекта нет дочерних элементов.

Примечания для тех, кто наследует этот метод

Классы, производные от , ContainerVisual должны реализовывать VisualChildrenCount свойство для перечисления визуальных дочерних элементов. Производное свойство должно возвращать количество дочерних элементов для ContainerVisual.

Визуальное дерево нельзя изменить во время этого вызова.

Применяется к

См. также раздел