FrameworkElement.Parent 屬性

定義

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

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

屬性值

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.

適用於

另請參閱