ContainerVisual.GetVisualChild(Int32) Metoda

Definicja

Zwraca określony element podrzędny Visual dla elementu nadrzędnego ContainerVisual.

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

Parametry

index
Int32

32-bitowa liczba całkowita ze znakiem, która reprezentuje wartość indeksu podrzędnego Visual. Wartość musi należeć do przedziału index od 0 do VisualChildrenCount -1.

Zwraca

Visual

Element podrzędny Visual.

Przykłady

W poniższym przykładzie pokazano, jak utworzyć ContainerVisual obiekt, który jest używany jako obiekt nadrzędny dla dwóch DrawingVisual obiektów. Obiekty dodawane do ContainerVisual obiektu muszą być dodawane w odwrotnej kolejności z (od dołu do góry), aby upewnić się, że są one renderowane w prawidłowej kolejności rysunku. W celu poprawnego wyliczenia drzewa wizualnego przykład zawiera zastępowane implementacje GetVisualChild metody i VisualChildrenCount właściwości.

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

Uwagi

Domyślnie element ContainerVisual nie ma żadnych elementów podrzędnych.

Uwagi dotyczące dziedziczenia

Nie można zmodyfikować drzewa wizualnego podczas tego wywołania.

Dotyczy

Zobacz też