共用方式為


ContainerVisual.GetVisualChild(Int32) 方法

定義

傳回父代 ContainerVisual 的指定子系 Visual

protected:
 override System::Windows::Media::Visual ^ GetVisualChild(int index);
protected override sealed System.Windows.Media.Visual GetVisualChild (int index);
override this.GetVisualChild : int -> System.Windows.Media.Visual
Protected Overrides NotOverridable Function GetVisualChild (index As Integer) As Visual

參數

index
Int32

32 位元帶正負號的整數,表示子系 Visual 的索引值。 index 的值必須介於 0 和 VisualChildrenCount - 1 之間。

傳回

Visual

子系 Visual

範例

下列範例示範如何建立做為兩 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;
    }
}
' 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 任何子系。

給繼承者的注意事項

無法在此呼叫期間修改視覺化樹狀結構。

適用於

另請參閱