FrameworkElement.RemoveLogicalChild(Object) 方法

定義

從這個項目的邏輯樹狀結構中移除提供的物件。 FrameworkElement 更新受影響的邏輯樹狀結構父指標,以便與刪除保持同步。

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)

參數

child
Object

要移除的項目。

範例

下列範例會在 Child 執行自己的視覺層實作的自訂 FrameworkElement 上實作 屬性。 屬性的 setter 是設計成,如果值變更,則會從邏輯樹狀結構中移除舊的值,以及類別特定的視覺效果集合。 系統會快取這些值,然後將新的值新增至標準 WPF 架構層級邏輯樹狀目錄和自訂視覺效果集合。

public virtual UIElement Child
{
    get
    {
        return _child;
    }
    set
    {
        if (_child != value)
        {
            //need to remove old element from logical tree
            if (_child != null)
            {
                OnDetachChild(_child);
                RemoveLogicalChild(_child);
            }

            _vc.Clear();

            if (value != null)
            {
                //add to visual
                _vc.Add(value);
                //add to logical
                AddLogicalChild(value);
            }

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

            //cache the new child
            _child = value;

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

            InvalidateMeasure();
        }
    }
}
<System.ComponentModel.DefaultValue(GetType(Object), Nothing)>
Public Overridable Property Child() As UIElement
    Get
        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
                OnDetachChild(_child)
                RemoveLogicalChild(_child)
            End If

            _vc.Clear()

            If value IsNot Nothing Then
                'add to visual
                _vc.Add(value)
                'add to logical
                AddLogicalChild(value)
            End If

            'always add the overlay child back into the visual tree if its set
            If _overlayVisual IsNot Nothing Then
                _vc.Add(_overlayVisual)
            End If

            'cache the new child
            _child = value

            'notify derived types of the new child
            If value IsNot Nothing Then
                OnAttachChild(_child)
            End If

            InvalidateMeasure()
        End If
    End Set
End Property

備註

使用這個方法,在代表專案邏輯子系的物件上實作集合。 這可能是在屬性 getter 或 setter、事件類別 Changed 處理常式、建構函式或集合類型本身中完成。

對於控制項作者,除非提供的基底控制項類別內容模型都不適用,否則在此層級操作邏輯樹狀結構並非建議的做法。 請考慮 、 ItemsControlHeaderedItemsControl 層級的 ContentControl 子類別。 這些類別提供透過專用 API 特別強制執行邏輯子系的內容模型,以及 WPF 控制項中通常想要的其他功能支援,例如透過範本設定樣式。

適用於

另請參閱