FrameworkElement.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

屬性值

此項目的邏輯父代。

範例

下列範例示範程式碼會檢查項目的父系,然後使用父代的屬性值來設定子項目上的屬性以符合。 在此情況下,這些是影響轉譯大小的屬性。

private void OnUIReady(object sender, System.EventArgs e)
{
    LinePane.Width = ((StackPanel)LinePane.Parent).ActualWidth;
    LinePane.Height = ((StackPanel)LinePane.Parent).ActualHeight;
    DesignerPane.MouseLeave += new System.Windows.Input.MouseEventHandler(DesignerPane_MouseLeave);
    this.SizeChanged += new SizeChangedEventHandler(Window1_SizeChanged);
}
Private Sub OnUIReady(ByVal sender As Object, ByVal e As System.EventArgs)
    LinePane.Width = (CType(LinePane.Parent, StackPanel)).ActualWidth
    LinePane.Height = (CType(LinePane.Parent, StackPanel)).ActualHeight
    AddHandler DesignerPane.MouseLeave, AddressOf DesignerPane_MouseLeave
    AddHandler SizeChanged, AddressOf Window1_SizeChanged
End Sub

備註

Parent 可能是 null 當元素具現化,但未附加至最後連接到頁面層級根項目或應用程式物件的任何邏輯樹狀結構時。

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

如需邏輯樹狀結構周遊的詳細資訊,請參閱 WPF 中的樹 狀結構,以及適當使用 Parent 作為父元素探索技術的情況。

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

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

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

請注意,如果這些屬性與邏輯樹狀結構父系不同,則不會報告視覺化樹狀結構父代。 一般應用程式案例通常不重要的視覺化樹狀結構父代,但可能是特定視覺層級案例所需的父元素。 請參閱 VisualTreeHelper

適用於

另請參閱