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

屬性值

此項目的邏輯父代。The logical parent for this element.

範例

下列範例會檢查 TextPointerParent 是否為特定類型。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.

變更專案的父系通常只會透過操作集合、使用專用的加入或移除方法,或透過設定元素的內容屬性來完成。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. 針對範本,最終會 null範本的 ParentFor 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.

適用於

另請參閱