FrameworkElement.RemoveLogicalChild(Object) 方法

定義

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

C#
protected internal void RemoveLogicalChild(object child);

參數

child
Object

要移除的項目。

範例

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

C#
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();
        }
    }
}

備註

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

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

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另請參閱