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


在实例化了元素但未附加到最终连接到页面级别根元素或应用程序对象的任何逻辑树的情况下,可能会 null ParentParent 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.

有关逻辑树遍历的详细信息以及使用 Parent 作为父元素发现技术的方案,请参阅WPF 中的树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.

通常仅通过操作集合、使用专用的 add 或 remove 方法或通过设置元素的内容属性来更改元素的父元素。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.

请注意,在这种情况下,此属性不会报告 visual tree 父元素。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.

Applies to

See also