ContainerVisual.VisualChildrenCount 属性

定义

获取 ContainerVisual 的子对象的数量。

protected override sealed int VisualChildrenCount { get; }

属性值

ContainerVisualVisualCollection 中的子对象的数量。

示例

下面的示例演示如何创建一个用作两DrawingVisualContainerVisual 对象的父对象的 对象。 ContainerVisual添加到对象的对象必须以反向 z 顺序添加 (从下到上) ,以确保它们以正确的绘制顺序呈现。 为了正确枚举可视化树,该示例提供了 方法和 VisualChildrenCount 属性的GetVisualChild重写实现。

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

注解

默认情况下, ContainerVisual 没有任何子级。

继承者说明

派生自 ContainerVisual 的类必须实现 VisualChildrenCount 属性才能枚举可视子级。 派生属性必须返回 的 ContainerVisual子级数。

在此调用期间,无法修改可视化树。

适用于

产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅