FrameworkElement.RemoveLogicalChild(Object) FrameworkElement.RemoveLogicalChild(Object) FrameworkElement.RemoveLogicalChild(Object) FrameworkElement.RemoveLogicalChild(Object) Method


從這個項目的邏輯樹狀結構中移除提供的物件。Removes the provided object from this element's logical tree. FrameworkElement 更新受影響的邏輯樹狀結構父指標,以便與刪除保持同步。FrameworkElement updates the affected logical tree parent pointers to keep in sync with this deletion.

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


Object Object Object Object

要移除的項目。The element to remove.


下列範例Child會在自訂FrameworkElement上, 執行其本身的視覺分層執行的屬性。The following example implements a Child property on a custom FrameworkElement that does its own visual layer implementation. 屬性的 setter 的設計是為了在值變更時, 會從邏輯樹狀結構中移除舊的值, 以及類別特定的視覺集合。The property's 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. 系統會快取這些值, 然後將新的值加入標準 WPF 架構層級邏輯樹狀結構和自訂視覺效果集合。The values are cached, and then the new value is added to both the standard WPF framework level 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 children of an element. 這可能是在屬性 getter 或 setter、事件、函式Changed的類別處理常式或集合類型本身中完成。This might be done in property getters or setters, class handlers 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 provided base control class' content models are appropriate. 請考慮在ContentControlItemsControlHeaderedItemsControl層級子類別化。Consider subclassing at the level of ContentControl, ItemsControl, and HeaderedItemsControl. 這些類別會提供內容模型, 並透過專用APIAPIs的方式強制執行邏輯子系, 並支援通常WPFWPF在控制項 (例如透過範本設定樣式) 中需要的其他功能。These classes provide a content model with particular enforcement of logical children through dedicated APIAPIs, as well as support for other features typically desirable in a WPFWPF control such as styling through templates.