FrameworkContentElement.Parent 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得該元素邏輯樹中的父節點。
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
屬性值
這個元素的邏輯母體。
範例
以下範例檢查 a 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。