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.

Notes to Inheritors

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).

Applies to