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)

参数

child
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
{
    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

注解

使用此方法可实现表示元素的逻辑子级的对象上的集合。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. 请考虑在ContentControlItemsControl和级别进行HeaderedItemsControl子类化。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.

适用于

另请参阅