FrameworkElement.OnApplyTemplate 方法


在衍生類別中被覆寫時,每當應用程式程式碼或內部處理序呼叫 ApplyTemplate() 時一都會叫用。When overridden in a derived class, is invoked whenever application code or internal processes call ApplyTemplate().

 virtual void OnApplyTemplate();
public virtual void OnApplyTemplate ();
abstract member OnApplyTemplate : unit -> unit
override this.OnApplyTemplate : unit -> unit
Public Overridable Sub OnApplyTemplate ()


這個方法沒有預設的實值。This method has no default implementation.

範本是元素完成的視覺化樹狀結構的區段,此樹狀結構是從針對專案套用之的範本屬性 StyleTemplates are the section of an element's completed visual tree that comes from the Template property of a Style that is applied for the element. 如需詳細資訊,請參閱設定 樣式和範本For more information, see Styling and Templating.


的衍生類別 FrameworkElement 可以使用此方法作為各種可能案例的通知:Derived classes of FrameworkElement can use this method as a notification for a variety of possible scenarios: -您可以呼叫您自己的程式碼執行,以建立元素視覺化樹狀結構的其餘部分。- You can call your own implementation of code that builds the remainder of an element visual tree.

-您可以從已套用的範本執行依賴視覺化樹狀結構的程式碼,例如從範本取得命名元素的參考。- You can run code that relies on the visual tree from templates having been applied, such as obtaining references to named elements that came from a template.

-您可以從範本的視覺化樹狀結構完成之後,引進只有意義的服務。- You can introduce services that only make sense to exist after the visual tree from templates is complete.

-您可以設定相依于其他因素之範本內元素的狀態和屬性。- You can set states and properties of elements within the template that are dependent on other factors. 比方說,屬性值可能只能透過知道父項目,或特定的衍生類別使用一般範本的方式來探索。For instance, property values might only be discoverable by knowing the parent element, or when a specific derived class uses a common template.

實作者應該在其本身的執行之前,一律呼叫基底實作為。Implementers should always call the base implementation before their own implementation. FrameworkElement 本身沒有預設的執行,但仲介類別可能。FrameworkElement itself has no default implementation, but intervening classes might.

Control 提供類似的覆寫 OnTemplateChanged(ControlTemplate, ControlTemplate)Control offers a similar override, OnTemplateChanged(ControlTemplate, ControlTemplate).