Partilhar via


Control Initialization and Layout

Microsoft Silverlight will reach end of support after October 2021. Learn more.

It is important to know the order of initialization and layout events and methods when you create a control. This way you know the correct method to override or event to handle to achieve a certain effect. Typically, a Silverlight control is constructed and its properties are set; next, its visual representation, or visual tree, is created; and the control then goes through the layout process. This following table lists the properties that are set and the events and methods that occur during control creation and layout. The properties, methods, and events are listed in the order in which they occur.

Control property, method, or event

Occurs

More information

Control is constructed.

In XAML, when the opening tag for a control declaration is parsed.

-or-

In code, when the constructor is explicitly called.

Getting Started with Controls

Control properties are set.

In XAML, when the XAML property attributes for a control are parsed.

-or-

In code, when the properties are explicitly set.

Getting Started with Controls

Style property is applied (explicit style).

In XAML, after the

closing tag for the control is parsed.

-or-

In code, when the Style property is explicitly set.

Getting Started with Controls

Style property is applied (default style from generic.xaml).

When the closing tag for the control is parsed, after the explicit style (if any) has been applied. The default style will not override the explicit style that was previously set.

Loaded event occurs.

When the element is added to the visual tree. Occurs before the next frame is rendered. This occurs before any layout operations.

Loaded

Template is applied (the control's visuals are created based on the template).

During the measure pass of layout. The value for the Template property, if it has been set, will be expanded to provide the visual tree. The control is initially created without a visual tree, and the visual tree is cleared when the Template property is set. You can also call ApplyTemplate to expand the template and create the visual tree for the control, if the control does not have a visual tree.

Control Customization

OnApplyTemplate method is called.

Whenever a control's template is applied. It is not necessary to call the base OnApplyTemplate for the template to be applied; however, inherited types might rely on it for their implementation.

Control Customization

Control visuals become available for manipulation in code.

In OnApplyTemplate. Call GetTemplateChild and pass the name of the template part to retrieve.

GetTemplateChild; VisualTreeHelper

MeasureOverride method is called.

In the first layout pass, when all the children are measured. If the template was expanded during this measure pass, MeasureOverride will be called after the template has been expanded.

Silverlight Layout System

ArrangeOverride method is called.

In the arrange layout pass, which occurs after the measure pass.

Silverlight Layout System

SizeChanged event is raised.

After both layout passes, measure and arrange, have completed.

Silverlight Layout System

LayoutUpdated event is raised.

If the SizeChanged event occurs.

Silverlight Layout System