FrameworkElement.AddLogicalChild(Object) Method


将提供的对象添加到此元素的逻辑树。Adds the provided object to the logical tree of this element.

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



要添加的子元素。Child element to be added.


下面的示例实现自定义 FrameworkElement 上的 Child 属性,该属性执行其自己的可视化层实现。The following example implements a Child property on a custom FrameworkElement that does its own visual layer implementation. 属性 setter 设计为这样,如果值发生更改,则会从逻辑树中删除旧值,并从特定于类的视觉对象集合中删除旧值。The property 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. 将缓存属性值,然后将新值添加到逻辑树和自定义视觉对象集合中。The property value is cached, and then the new value is added to both the 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 child elements of an element. 子元素集合的集合维护可以在属性 getter 或 setter 中完成,也可以在更改事件、构造函数或集合类型本身的类处理中完成。Collection maintenance for child element collections might be done in property getters or setters, class handling 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 content models for available base control classes are appropriate for your control scenario. 请考虑在 ContentControlItemsControlHeaderedItemsControl级别进行子类化。Consider subclassing at the level of ContentControl, ItemsControl, and HeaderedItemsControl. 这些类提供了一个内容模型,其中包含通过专用 Api 特定的逻辑树子元素强制,并支持在 WPFWPF 控件(例如通过模板设置样式)中通常需要的其他功能。These classes provide a content model with particular enforcement of logical tree child elements through dedicated APIs, as well as support for other features typically desirable in a WPFWPF control such as styling through templates. 有关如何使用 LogicalChildrenAddLogicalChild的详细信息,请参阅WPF 中的树For more information on how to use LogicalChildren and AddLogicalChild, see Trees in WPF.

如果在另一个进程循环访问逻辑树时调用,则 AddLogicalChild 可能会引发异常。AddLogicalChild may throw an exception if called at a time when the logical tree is being iterated by another process.

Applies to

See also