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)



要删除的元素。The element to remove.


下面的示例实现自定义 FrameworkElement 上的 Child 属性,该属性执行其自己的可视化层实现。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. 这些类提供了一个内容模型,其中包含通过专用 Api 特定的逻辑子级强制,并支持在 WPFWPF 控件中通常需要的其他功能,例如通过模板进行样式设置。These classes provide a content model with particular enforcement of logical children through dedicated APIs, as well as support for other features typically desirable in a WPFWPF control such as styling through templates.

Applies to

See also