Visual.AddVisualChild(Visual) 메서드

정의

두 시각적 개체 사이의 부모/자식 관계를 정의합니다.

protected:
 void AddVisualChild(System::Windows::Media::Visual ^ child);
protected void AddVisualChild (System.Windows.Media.Visual child);
member this.AddVisualChild : System.Windows.Media.Visual -> unit
Protected Sub AddVisualChild (child As Visual)

매개 변수

child
Visual

부모 시각적 개체에 추가할 자식 시각적 개체입니다.

예제

다음 예제에서는 시각적 자식 개체의 사용자 지정 스토리지 요구 사항을 정의하는 방법을 보여 줍니다. 이 예제에서는 및 RemoveVisualChild 메서드를 AddVisualChild 사용하여 부모 시각적 개체와 child부모 시각적 개체 간의 부모-자식 관계를 설정합니다. 시각적 트리가 올바르게 열거되도록 하기 위해 이 예제에서는 메서드 및 VisualChildrenCount 속성의 재정의된 구현을 GetVisualChild 제공합니다.

참고

VisualCollection을 사용하여 시각적 개체 간의 부모-자식 관계를 만들 수는 있지만 한 자식만 부모에 연결된 경우 고유한 사용자 지정 스토리지 구현을 제공하는 것이 더 효율적입니다.

// Create a host visual derived from the FrameworkElement class.
// This class provides layout, event handling, and container support for
// the child visual object.
public class MyVisualHost : FrameworkElement
{
    private DrawingVisual _child;

    public MyVisualHost(DrawingVisual drawingVisual)
    {
        _child = drawingVisual;
        this.AddVisualChild(_child);
    }

    public DrawingVisual Child
    {
        get
        {
            return _child;
        }

        set
        {
            if (_child != value)
            {
                this.RemoveVisualChild(_child);
                _child = value;
                this.AddVisualChild(_child);
            }
        }
    }

    // Provide a required override for the VisualChildrenCount property.
    protected override int VisualChildrenCount
    {
        get { return _child == null ? 0 : 1; }
    }

    // Provide a required override for the GetVisualChild method.
    protected override Visual GetVisualChild(int index)
    {
        if (_child == null)
        {
            throw new ArgumentOutOfRangeException();
        }

        return _child;
    }
' Create a host visual derived from the FrameworkElement class.
' This class provides layout, event handling, and container support for
' the child visual object.
Public Class MyVisualHost
    Inherits FrameworkElement
    Private _child As DrawingVisual

    Public Sub New(ByVal drawingVisual As DrawingVisual)
        _child = drawingVisual
        Me.AddVisualChild(_child)
    End Sub

    Public Property Child() As DrawingVisual
        Get
            Return _child
        End Get

        Set(ByVal value As DrawingVisual)
            If _child IsNot value Then
                Me.RemoveVisualChild(_child)
                _child = value
                Me.AddVisualChild(_child)
            End If
        End Set
    End Property

    ' Provide a required override for the VisualChildrenCount property.
    Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
        Get
            Return If(_child 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 _child Is Nothing Then
            Throw New ArgumentOutOfRangeException()
        End If

        Return _child
    End Function

설명

이 메서드는 AddVisualChild 두 시각적 개체 간의 부모-자식 관계를 설정합니다. 이 메서드는 시각적 자식 개체의 기본 스토리지 구현에 대한 낮은 수준 제어가 필요할 때 사용해야 합니다. VisualCollection 자식 개체를 저장하기 위한 기본 구현으로 사용할 수 있습니다.

적용 대상

추가 정보