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

Property Value


此項目的邏輯父代。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. 針對範本,最終會 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.

請注意,這個屬性不會在它們與邏輯樹狀目錄父系不同的情況下,報告視覺化樹狀目錄的父代。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. 請參閱<VisualTreeHelper>。See VisualTreeHelper.

Applies to

See also