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. 請注意,這些情況不一定會引發例外狀況,因為可能會有依賴 reparenting 專案的設計工具案例,而這些專案不會有其「一般」的父代。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.