FrameworkContentElement.Parent 属性

定义

获取此元素的逻辑树中的父级。Gets the parent in the logical tree for this element.

public:
 property System::Windows::DependencyObject ^ Parent { System::Windows::DependencyObject ^ get(); };
public System.Windows.DependencyObject Parent { get; }
member this.Parent : System.Windows.DependencyObject
Public ReadOnly Property Parent As DependencyObject

属性值

DependencyObject

此元素的逻辑父级。The logical parent for this element.

示例

下面的示例检查的是否 Parent TextPointer 为特定类型。The following example checks to see whether the Parent of a TextPointer is of a particular type.

// Traverse content in forward direction until the position is immediately after the opening 
// tag of a Run element, or the end of content is encountered.
while (position != null)
{
    // Is the current position just after an opening element tag?
    if (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
    {
        // If so, is the tag a Run?
        if (position.Parent is Run)
            break;
    }

    // Not what we're looking for; on to the next position.
    position = position.GetNextContextPosition(LogicalDirection.Forward);
}
' Traverse content in forward direction until the position is immediately after the opening 
' tag of a Run element, or the end of content is encountered.
Do While position IsNot Nothing
    ' Is the current position just after an opening element tag?
    If position.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.ElementStart Then
        ' If so, is the tag a Run?
        If TypeOf position.Parent Is Run Then
            Exit Do
        End If
    End If

    ' Not what we're looking for on to the next position.
    position = position.GetNextContextPosition(LogicalDirection.Forward)
Loop

注解

请注意,根据应用程序功能,元素的逻辑父级可能会发生更改,并且保持此属性的值将不会反映此更改。Note that the logical parent of an element can potentially change depending on your application functionality, and keeping the value of this property will not reflect that change. 通常应在需要之前立即获取值。You typically should get the value immediately before you need it.

有关遍历逻辑树的详细信息,请参阅 WPF 中的树 和适用于元素发现的情况。See Trees in WPF for more information about traversing logical trees, and the scenarios where taking this approach towards element discovery is appropriate.

属性系统在重定父级后可能会重新计算某个元素的所有属性值,因为某些属性通过逻辑树继承值。The property system will potentially recalculate all property values of an element when it is reparented, because some properties inherit values through the logical tree. DataContext当对元素进行重定父级时,适用于绑定的也可能更改。The DataContext that applies for bindings can also change when elements are reparented.

通常仅通过操作集合、使用专用的 add 或 remove 方法或通过设置元素的内容属性来更改元素的父元素。Changing an element's parent is typically only done through manipulation of collections, by using dedicated add or remove methods, or through setting content properties of elements.

使用属性的最典型方案 Parent 是获取引用,然后 FrameworkContentElement 从父级获取各种属性值。The most typical scenario for using the Parent property is to obtain a reference and then get various FrameworkContentElement property values from the parent. 对于模板, Parent 模板的最终将是 nullFor templates, the Parent of the template eventually will be null. 若要过去此点并扩展到实际应用模板的逻辑树,请使用 TemplatedParentTo get past this point and extend into the logical tree where the template is actually applied, use TemplatedParent.

适用于

另请参阅