FrameworkElement.OnVisualParentChanged(DependencyObject) FrameworkElement.OnVisualParentChanged(DependencyObject) FrameworkElement.OnVisualParentChanged(DependencyObject) FrameworkElement.OnVisualParentChanged(DependencyObject) Method


在視覺化樹狀結構中這個項目的父項目變更時叫用。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)


DependencyObject DependencyObject DependencyObject DependencyObject

舊的父項目。The old parent element. 可能是 null,表示項目之前沒有視覺父項目。May be null to indicate that the element did not have a visual parent previously.


視覺化樹狀結構是有別於邏輯樹狀結構,因為後者省略不 (例如集合),以視覺化的方式,呈現的項目,並展開其主題和樣式複合 (compositing) 根據某些項目。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,接著呼叫其基底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. 此案例中的特製化的版本存在於清單項目和功能表項目,其中沒有意義的任何父代之外 visual 擁有適當的集合,來儲存它們的實作。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.

這個方法也會覆寫中特定項目,它們通常是根項目,例如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). WindowPage實作刻意密封方法。The Window and Page implementations deliberately seal the method.