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.


這個虛擬方法的預設執行會查詢新的父系、引發各種初始化事件,以及視需要設定的初始化狀態相關的內部旗標 FrameworkElementThe 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 ,進而在中呼叫其基底 VisualFinally, 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.

這種方法也會在通常是根項目的特定元素中覆寫,例如 WindowThis 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). 和執行會刻意 Window Page 密封方法。The Window and Page implementations deliberately seal the method.