Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.OnApplyTemplate Method

When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) call ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays in an application. For more information, see Remarks.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Public Overridable Sub OnApplyTemplate

Windows Phone template support is not fully enabled on FrameworkElement. Instead, the Windows Phone architecture provides template support for Control, which is an immediate subclass of FrameworkElement. Specialized template behavior also exists for ContentPresenter.

Notes to Inheritors

This method can have a native implementation, which is referenced by the default implementation at the FrameworkElement level; therefore, you should always call the base implementation if deriving from FrameworkElement. If deriving from Control or further subclasses, the Control implementation calls the FrameworkElement base. Therefore, this accesses the native-level implementation implicitly if you call the immediate base.

Derived classes can use this method as a notification or entry point for the following scenarios:

  • Build the remainder of a visual tree using custom code.

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

  • Introduce services that only make sense to exist after the visual tree from templates is complete.

  • Attach class-defined event handlers to parts of the template. For example, you might want class logic to handle KeyDown events from a TextBox template part so that UI states are updated, and other events that are specific to your control are raised instead.

  • 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. However, note that a well-designed control should generally handle its visual and behavioral states through VisualStateManager.

OnApplyTemplate is often a more appropriate point to deal with adjustments to the template-created visual tree than is the Loaded event. In Windows Phone, the Loaded event might occur before the template is applied, and therefore, you might not be able to adjust the visual tree that is created through applying a template in a Loaded handler. For more information, see the Remarks for Loaded.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2015 Microsoft