FrameworkElement.OnVisualParentChanged(DependencyObject) 方法


当此元素父级在可视化树中发生更改时进行了调用。Invoked when the parent of this element in the visual tree is changed. 重写 OnVisualParentChanged(DependencyObject)Overrides OnVisualParentChanged(DependencyObject).

protected public:
 override void OnVisualParentChanged(System::Windows::DependencyObject ^ oldParent);
protected internal override void OnVisualParentChanged (System.Windows.DependencyObject oldParent);
override this.OnVisualParentChanged : System.Windows.DependencyObject -> unit
Protected Friend Overrides Sub OnVisualParentChanged (oldParent As DependencyObject)



旧的父元素。The old parent element. 可能是 null,指示元素之前没有可视父级。May be null to indicate that the element did not have a visual parent previously.


可视化树可能不同于逻辑树,因为它会忽略不以视觉方式呈现的元素(例如集合),并根据其主题和样式组合展开某些元素。The visual tree is potentially different from the logical tree because it omits elements that do not render visually, such as collections, and expands some elements on the basis of their theme and style compositing. 有关详细信息,请参见 WPF 中的树For more information, see Trees in WPF.


此虚方法的默认实现将查询新父代,引发各种初始化事件,并根据需要设置有关 FrameworkElement 初始化状态的内部标志。The default implementation of this virtual method queries for the new parent, raises various initialization events, and sets internal flags about initialization state of the FrameworkElement as appropriate. 最后,它将调用由 UIElement声明的后续基本实现,而后者又会在 Visual中调用其基础。Finally, it calls the successive base implementations as declared by UIElement, which in turn calls its base in Visual. 始终调用基实现以保留此行为,否则,如果声明为另一个元素的子元素,则此元素的元素树行为可能不符合预期。Always call the base implementation to preserve this behavior, otherwise the element tree behavior for this element when declared as a child of another element may not be as expected.

一些现有 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 类会重写此方法,例如: OnVisualParentChanged(DependencyObject)OnVisualParentChanged(DependencyObject)A few existing Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) classes override this method, for example: OnVisualParentChanged(DependencyObject), OnVisualParentChanged(DependencyObject). 最常见的方案是强制新的父项必须是特定类型。The most common scenario is to enforce that the new parent must be a particular type. 如果新父级未能通过某种方式进行类型测试,这可能涉及到引发异常。This might involve throwing an exception if the new parent failed some manner of type test. 此方案的专用版本存在于列表项和菜单项的实现中,这些列表项和菜单项在拥有适当集合以存储它们的父视觉对象之外没有任何意义。A specialized version of this scenario exists in implementations for list items and menu items, which do not make any sense outside a parent visual that owns an appropriate collection to store them in. 请注意,这种情况并不一定会引发异常,因为可能存在依赖于重新设置父级元素的设计器方案,这些元素暂时没有 "常规" 父级。Note that these cases do not necessarily raise exceptions, because there might be designer scenarios that rely on reparenting elements that are momentarily without their "regular" parents.

此方法也会在通常为根元素的某些元素(如 Window)中重写。This method is also overridden in certain elements that are typically the root element, such as Window. 另一种情况是元素,这些元素是标记中的元素,但会在编译的逻辑树中自动生成更大的基础结构(如 Page)。Another case is elements that are the apparent root element in markup but which autogenerate a greater infrastructure in a compiled logical tree (such as Page). WindowPage 实现有意密封方法。The Window and Page implementations deliberately seal the method.