FrameworkElement.AddLogicalChild(Object) FrameworkElement.AddLogicalChild(Object) FrameworkElement.AddLogicalChild(Object) FrameworkElement.AddLogicalChild(Object) Method


將所提供的物件加入至此項目的邏輯樹狀結構。Adds the provided object to the logical tree of this element.

protected public:
 void AddLogicalChild(System::Object ^ child);
protected internal void AddLogicalChild (object child);
member this.AddLogicalChild : obj -> unit
Protected Friend Sub AddLogicalChild (child As Object)


Object Object Object Object

要加入的子項目。Child element to be added.


下列範例會實作Child自訂屬性FrameworkElement執行它自己的視覺分層實作。The following example implements a Child property on a custom FrameworkElement that does its own visual layer implementation. 屬性 setter 經過設計,如果值變更時,從邏輯樹狀結構,以及特定類別的視覺集合移除的舊值。The property setter is designed so that if the value changes, the old value is removed from the logical tree, as well as a class-specific visual collection. 快取的屬性值時,,然後新增至邏輯樹狀結構和自訂的視覺集合的值。The property value is cached, and then the new value is added to both the logical tree and the custom visual collection.

public virtual UIElement Child
        return _child;
        if (_child != value)
            //need to remove old element from logical tree
            if (_child != null)


            if (value != null)
                //add to visual
                //add to logical

            //always add the overlay child back into the visual tree if its set
            if (_overlayVisual != null)

            //cache the new child
            _child = value;

            //notify derived types of the new child
            if (value != null)

<System.ComponentModel.DefaultValue(GetType(Object), Nothing)>
Public Overridable Property Child() As UIElement
        Return _child
    End Get
    Set(ByVal value As UIElement)
        If _child IsNot value Then
            'need to remove old element from logical tree
            If _child IsNot Nothing Then
            End If


            If value IsNot Nothing Then
                'add to visual
                'add to logical
            End If

            'always add the overlay child back into the visual tree if its set
            If _overlayVisual IsNot Nothing Then
            End If

            'cache the new child
            _child = value

            'notify derived types of the new child
            If value IsNot Nothing Then
            End If

        End If
    End Set
End Property


使用此方法實作的物件,表示項目的邏輯子項目的集合。Use this method for the implementation of collections on objects that represent logical child elements of an element. 子項目集合的集合維護可能會在屬性 getter 或 setter,類別處理的 Changed 事件,建構函式,或將集合型別本身內完成。Collection maintenance for child element collections might be done in property getters or setters, class handling of Changed events, constructors, or within the collection types themselves.

針對控制項作者,操作邏輯樹狀結構,在此層級不是建議的作法,除非使用的基底控制項類別的內容模型都適用於您控制的案例。For control authors, manipulating the logical tree at this level is not the recommended practice, unless none of the content models for available base control classes are appropriate for your control scenario. 層級的子類別化,請考慮ContentControlItemsControl,和HeaderedItemsControlConsider subclassing at the level of ContentControl, ItemsControl, and HeaderedItemsControl. 這些類別提供特定的強制執行的邏輯樹狀結構子項目,透過專用的內容模型APIAPIs,以及通常希望出現在其他功能的支援WPFWPF控制項,例如透過範本的樣式。These classes provide a content model with particular enforcement of logical tree child elements through dedicated APIAPIs, as well as support for other features typically desirable in a WPFWPF control such as styling through templates. 如需有關如何使用LogicalChildrenAddLogicalChild,請參閱WPF 中的樹狀結構For more information on how to use LogicalChildren and AddLogicalChild, see Trees in WPF.

AddLogicalChild 如果當邏輯樹狀結構會進行逐一查看另一個處理序一次呼叫,則可能會擲回例外狀況。AddLogicalChild may throw an exception if called at a time when the logical tree is being iterated by another process.