다음을 통해 공유


방법: 논리 트리 재정의

일반적으로는 필요하지 않지만 숙련된 컨트롤 작성자의 경우에는 필요에 따라 논리 트리를 재정의할 수 있습니다.

예제

이 예제에서는 StackPanel을 서브클래싱하여 패널이 자식 요소를 하나만 포함하고 렌더링하는 동작을 적용하기 위해 논리 트리를 재정의하는 방법을 설명합니다. 이 동작은 일반적으로 권장되지는 않지만 요소의 일반적인 논리 트리를 재정의하는 시나리오를 보여 주기 위해 이 예제에 사용되었습니다.

    Public Class SingletonPanel
        Inherits StackPanel
        'Private _children As UIElementCollection 
        Private _child As FrameworkElement

        Public Sub New()

        End Sub

        Public Property SingleChild() As FrameworkElement

            Get
                Return _child
            End Get
            Set(ByVal value As FrameworkElement)
                If value Is Nothing Then
                     RemoveLogicalChild(_child)
                Else
                     If _child Is Nothing Then
                         _child = value
                     Else
                         ' raise an exception?
                         MessageBox.Show("Needs to be a single element")
                     End If
                End If
            End Set
        End Property
        Public Sub SetSingleChild(ByVal child As Object)
            Me.AddLogicalChild(child)
        End Sub

        Public Shadows Sub AddLogicalChild(ByVal child As Object)
            _child = CType(child, FrameworkElement)
            If Me.Children.Count = 1 Then
                Me.RemoveLogicalChild(Me.Children(0))
                Me.Children.Add(CType(child, UIElement))
            Else
                Me.Children.Add(CType(child, UIElement))
            End If
        End Sub

        Public Shadows Sub RemoveLogicalChild(ByVal child As Object)

            _child = Nothing
            Me.Children.Clear()
        End Sub
        Protected Overrides ReadOnly Property LogicalChildren() As IEnumerator
           Get
           ' cheat, make a list with one member and return the enumerator
           Dim _list As New ArrayList()
           _list.Add(_child)
           Return CType(_list.GetEnumerator(), IEnumerator)
           End Get
        End Property
    End Class
public class SingletonPanel : StackPanel
{
    //private UIElementCollection _children; 
    private FrameworkElement _child;

    public SingletonPanel() {

    }

    public FrameworkElement SingleChild
    {

        get { return _child;}
        set
        {
            if (value==null) {
                 RemoveLogicalChild(_child);
            } else {             
                 if (_child==null) {
                     _child = value;
                 } else {
                     // raise an exception?
                     MessageBox.Show("Needs to be a single element");
                 }
            }
        } 
    }
    public void SetSingleChild(object child)
    {
        this.AddLogicalChild(child);
    }

    public new void AddLogicalChild(object child)
    {
        _child = (FrameworkElement)child;
        if (this.Children.Count == 1)
        {
            this.RemoveLogicalChild(this.Children[0]);
            this.Children.Add((UIElement)child);
        }
        else
        {
            this.Children.Add((UIElement)child);
        }
    }

    public new void RemoveLogicalChild(object child)

    {
        _child = null;
        this.Children.Clear();
    }
    protected override IEnumerator LogicalChildren
    {
       get {
       // cheat, make a list with one member and return the enumerator
       ArrayList _list = new ArrayList();
       _list.Add(_child);
       return (IEnumerator) _list.GetEnumerator();}
    }
}

논리 트리에 대한 자세한 내용은 WPF의 트리를 참조하십시오.