FrameworkElement.OnApplyTemplate FrameworkElement.OnApplyTemplate FrameworkElement.OnApplyTemplate FrameworkElement.OnApplyTemplate Method


在衍生類別中被覆寫時,每當應用程式程式碼或內部處理序呼叫 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.

範本是專案已完成的視覺化樹狀結構中的區段, 來自針對專案所套用之Style的範本屬性。Templates 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).