Editar

Compartir vía


FrameworkContentElement.Parent Property

Definition

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

Property Value

The logical parent for this element.

Examples

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

Remarks

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.

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. The DataContext that applies for bindings can also change when elements are reparented.

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.

The most typical scenario for using the Parent property is to obtain a reference and then get various FrameworkContentElement property values from the parent. For templates, the Parent of the template eventually will be null. To get past this point and extend into the logical tree where the template is actually applied, use TemplatedParent.

Applies to

See also