Inline Styles and Templates

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

Styles and templates are two concepts used by both Silverlight and WPF. In Silverlight, styles and templates are always defined in XAML. In some cases, it is more appropriate to define a style or template once in a resource dictionary and refer to it as a resource. In other cases, it might be appropriate to define the style or template as an inline value rather than a resource. This topic discusses the relative merits of resources versus inline definitions, particularly for certain style or template usages.

Inline Styles for the FrameworkElement.Style Property

Declaring a Style as an inline object element is not a typical technique for setting the Style property of a single control instance. Properties set in this way could just as easily be set directly as attributes directly on the object element that defines the instance, or with property element usages. Also, styles are often intended for re-use. To make the style shareable, you would define them as resources in a ResourceDictionary and set one or more Style property values with a StaticResource reference, rather than defining the style inline.

Styles for Properties of Individual Controls

Certain controls provide properties that are of type Style. The intention of these Style properties is not to replace the entire style, or to replace the entire template. Instead, the specific style properties are intended for changing the style of a particular part of the control, without requiring you to copy and replace the full control template to do so. For example, the Calendar control exposes several style properties that can set the individual styles for the days and months within the calendar grid. For these cases, where there is a dedicated property within the control model of type Style, it can be appropriate to define that style inline. Whether to define the style inline or as another named resource is a matter of preference, and will depend on how you choose to factor your application XAML.

Inline Templates

The control template that defines the visual tree for a control is typically not a template you would define inline. But there are other types of templates where defining the template inline as an instance property value might be appropriate. One such case is a DataTemplate. The purpose of a DataTemplate is to format the bound data, and it might be that you only need that particular data formatting scheme in one part of your application. A similar scenario is for an ItemsControl, where you use Items to provide the input from data and use an ItemTemplate.

Note that the Control.Template property value that you set to define a template for an implicit style is not an example of an "inline template" as is discussed in this topic. In that case, you are setting the value of the style's template for all applications of the style, and setting the Control.Template value as a property element of the style is the necessary syntax for achieving that scenario. For more information on control templates, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.