.NET Framework Class Library for Silverlight
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.

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

Visual Basic (Declaration)
Public Overridable Sub OnApplyTemplate
Visual Basic (Usage)
Dim instance As FrameworkElement

instance.OnApplyTemplate()
C#
public virtual void OnApplyTemplate()
Remarks

Silverlight template support is not fully enabled on FrameworkElement. Instead, the Silverlight architecture provides template support for Control, which is an immediate subclass of FrameworkElement. Specialized template behavior also exists for ContentPresenter. OnApplyTemplate is defined at the FrameworkElement level for WPF compatibility reasons.

Notes to Inheritors:

This method potentially has a native implementation (which is referenced by the default implementation at the FrameworkElement level), so you should always call the base implementation if deriving from FrameworkElement. If deriving from Control or further subclasses, the Control implementation will call the FrameworkElement base. This will thus access the native-level implementation implicitly, so long as you call the immediate base.

Derived classes can use this method as a notification/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.

  • 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. For details on this concept, see Control Customization.

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

Examples

The following example shows an OnApplyTemplate override defined by a custom control that is designed to be templateable. As part of its definition, the control attributes the named elements within a template that are required, such as UpButtonElement. Then OnApplyTemplate retrieves the object references based on this naming contract when the template is loaded. Also, this example calls the private method UpdateState (definition not shown). This is another common scenario for OnApplyTemplate: making sure that the visual state is set for the control's starting state, in this case by calling a private method that accounts for all of the control's defined states and calls GoToState to set the appropriate state.

Visual Basic
Public Overloads Overrides Sub OnApplyTemplate()
    UpButtonElement = TryCast(GetTemplateChild("UpButton"), RepeatButton)
    DownButtonElement = TryCast(GetTemplateChild("DownButton"), RepeatButton)
    TextElement = TryCast(GetTemplateChild("TextBlock"), TextBlock)

    UpdateStates(False)
End Sub
C#
public override void OnApplyTemplate()
{
    UpButtonElement = GetTemplateChild("UpButton") as RepeatButton;
    DownButtonElement = GetTemplateChild("DownButton") as RepeatButton;
    TextElement = GetTemplateChild("TextBlock") as TextBlock;

    UpdateStates(false);
}
Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags :


Page view tracker