FrameworkContentElement.Parent 屬性

定義

取得此項目之邏輯樹狀中的父代。

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

屬性值

此項目的邏輯父代。

範例

下列範例會檢查 的 TextPointer 是否 Parent 為特定型別。

// 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

備註

請注意,元素的邏輯父代可能會根據應用程式功能而變更,而且保留此屬性的值不會反映該變更。 您通常應該在需要此值之前立即取得此值。

如需周遊邏輯樹狀結構的詳細資訊,請參閱 WPF 中的 樹狀結構,以及採用此方法進行元素探索的案例。

屬性系統可能會在重新父代時重新計算專案的所有屬性值,因為某些屬性會透過邏輯樹狀結構繼承值。 套 DataContext 用至系結的 ,也可以在重新父系專案時變更。

變更專案的父代通常只能透過操作集合、使用專用的新增或移除方法,或透過設定元素的內容屬性來完成。

使用 Parent 屬性的最典型案例是取得參考,然後從父系取得各種 FrameworkContentElement 屬性值。 對於範本, Parent 範本的 最終會是 null 。 若要超過這個點並延伸至實際套用範本的邏輯樹狀結構,請使用 TemplatedParent

適用於

另請參閱