Element. On Visual Parent Changed(DependencyObject) Method
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Invoked when the parent of this element in the visual tree is changed. 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. 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. For more information, see Trees in WPF.
Notes to Inheritors
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. 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.
A few existing 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.
This method is also overridden in certain elements that are typically the root element, such as Window. 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). The Window and Page implementations deliberately seal the method.