FrameworkElement.Parent FrameworkElement.Parent FrameworkElement.Parent FrameworkElement.Parent Property


取得這個項目的邏輯父項目。Gets the logical parent element of this element.

 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


此項目的邏輯父代。This element's logical parent.


下列範例會顯示項目的父代,會檢查,然後再使用從父代的屬性值來比對的子系項目上設定屬性的程式碼。The following example shows code that checks for an element's parent, and then uses property values from the parent to set properties on the child element to match. 在此情況下,這些是會影響轉譯大小的屬性。In this case these are properties that affect the rendering size.

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中的項目已具現化,而未附加到任何最終會連接到的頁面層級的根項目中或應用程式物件的邏輯樹狀結構的情況。Parent may be null in cases where an element was instantiated, but is not attached to any logical tree that eventually connects to the page level root element, or the application object.

請注意,根據您的應用程式的功能,有可能變更邏輯父項目保留這個屬性的值並不會反映該變更。Note that the logical parent of an element can potentially change depending on your application's 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 中的樹狀結構邏輯樹狀結構周遊的詳細資訊和案例,使用Parent適當探索是這項技術的父項目。See Trees in WPF for more information about logical tree traversal, and the scenarios where using Parent as a technique of parent element discovery is appropriate.

屬性引擎有可能重新計算之元素的所有屬性值時,它會重設父代,因為有些屬性繼承邏輯樹狀結構的值。The property engine 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屬性是要取得的參考,然後以取得各種FrameworkElement從父代的屬性值。The most typical scenario for using the Parent property is to obtain a reference and then get various FrameworkElement property values from the parent. 如需範本,Parent範本的最終會nullFor 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.

請注意這個屬性不會報告從邏輯樹狀結構父項的變化的案例中的視覺化樹狀結構父代。Note that this property does not report visual tree parents in cases where these vary from the logical tree parents. 視覺化樹狀結構父代不是一般的應用程式的情況下通常特別重要,但可能會針對某些視覺層級的情況下所需的父項目。Visual tree parents are not typically important for general application cases but may be the desired parent elements for certain visual level cases. 請參閱 VisualTreeHelperSee VisualTreeHelper.