This article is written by Alexander Golesh (Silverlight MVP).
This is introduction to Silverlight styling and templating features. This is also the fifth chapter of “Silverlight Tutorials for Beginners” series.
Content
- Styling
- Templating
- Conclusion
Styling
Silverlight controls has default look and feel, which is very close to standard Windows application look and feel.

Style is a mechanism in Silverlight that allows you to encapsulate control property values as reusable resources. This is conceptually similar to CSS with HTML. Style defines control property values as reusable resources.
The same button which was used at figure 1 could have pretty non-standard look-and-feel.

Style also allows you to achieve same look-and-feel for multiple controls of the same type across all the application.
Let’s see how to create the style.
Let’s create a simple button defining Button element in XAML inside the StackPanel:
<Button Content="First Button" Width="100" Height="25"/>
Style defined in “Style” tag in XAML file as a part of some content resources. Placing a style inside a specific XAML page will enable its use in this specific content, while placing it in App.xaml will enable it to be reused across all pages and controls in application. Style also could be placed inside specific panel element, which will allow using this style only to the panel’s child elements. The style defined as a part of the Resources for chosen element.
Sample style definition:
<StackPanel.Resources>
<Style TargetType="Button" x:Key="btnStyle">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Background" Value="Blue"/>
</Style>
</StackPanel.Resources>
In this case we defined the style as a part of some StackPanel, which will allow using this style only for child’s of this StackPanel. The style has 2 required attributes:
- TargetType – the Type of the element which allowed using this style. The type itself could specify the base class for the group of elements, for example ButtonBase – specifying it as TargetType enables to use this style for any derived class, like Button, RadioButton, CheckBox, etc.
- x:Key – the name of the style, which will be used by the elements to access the style.
Style defines the properties of the target element by specifying property name and the desired value in “Setter” element. In our case the style define values of 3 properties for the Button: Width, Height and Background.
Now it is a time to apply this style on our button:
<Button Content="First Button" Width="100" Height="25" Style="{StaticResource btnStyle}"/>
Style applied by specifying “Style” attribute of the desired element. Style (and not only style) specification uses special syntax called Markup Extension. This syntax is merely just a specification of some special command (in opposite of the attribute value, which usually being placed to the attribute) – in our case it is “StaticResource”. To distinguish special syntax from regular value it being placed inside curly braces “{…}”.
In our case StaticResource means that XAML parser should look for the resource named btnStyle and use the value of this resource as the value for the attribute.

Now our button has specified look. We could use this resource as many times as we need inside the scope of our style definition, and all elements with the same style will have same look and feel.

If we will change the definition in the style, all elements with this style applied will get the change:
<Setter Property="Background" Value="Green"/>

Any element could specify any property value at the element definition, and it will override the value given by style (if applied)
<Button Content="First Button" Width="100" Height="25" Style="{StaticResource btnStyle}" Background="Red"/>
<Button Content="Second Button" Width="100" Height="25" Style="{StaticResource btnStyle}"/>
<Button Content="Third Button" Width="100" Height="25" Style="{StaticResource btnStyle}"/>

Style could “derive” from another style by using “BasedOn” attribute.
Let’s see how to define style hierarchy for the buttons, and how it behaves:
<Style x:Key="BaseButtonStyle" TargetType="Button">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Margin" Value="5"/>
</Style>
<Style x:Key="DerivedButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="3"/>
</Style>
<Style x:Key="ExtendedButtonStyle" TargetType="Button" BasedOn="{StaticResource DerivedButtonStyle}">
<Setter Property="ClickMode" Value="Release"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Height" Value="35"/>
<Setter Property="Width" Value="150"/>
<Setter Property="Background" Value="Green"/>
</Style>
In this sample we created 3 styles for the Button type: ButtonBaseStyle, DerivedButtonStyle and ExtendedButtonStyle.
The DerivedButtonStyle uses BasedOn attribute to derive from ButtonBaseStyle. The BasedOn defined by using same Markup Extension syntax to define the parent style BasedOn=”{StaticResource BaseButtonStyle}”. The DerivedButtonStyle got all the properties defined in base style and adds 3 new properties. The ExtendedButtonStyle uses BasedOn attribute to derive from DerivedButtonStyle. It gets all the properties defined in DerivedButtonStyle and in ButtonBaseStyle, adds new property definitions and also overrides some of properties defined in base classes: Width, Height and Background.
Applied to the button will give use following look and feel:

The existing style could be applied not only by using Markup Extension in XAML, but also form code behind:
Style style = this.Resources["BaseButtonStyle"] as Style;
if (null != style)
btnSimple.Style = style;
The style itself also could be defined in code behind.
Let’s change the style of the button on the click event. The button definition:
<Button Content="Set Style" Click="Button_Click" x:Name="btnNewButton" Width="100"/>
The initial look:

The event handler code:
private void Button_Click(object sender, RoutedEventArgs e)
{
Style myStyle = new Style();
myStyle.TargetType = typeof(Button);
Setter setter = new Setter();
setter.Property = Button.BackgroundProperty;
setter.Value = new SolidColorBrush(Colors.Yellow);
myStyle.Setters.Add(setter);
btnNewButton.Style = myStyle;
}
And after the code being executed the button gets following look:

The styling is powerful feature which allows us to configure basic set of properties for the element. But how it helps us to create the button which looks like this:

The colors and fonts – we already know. But what about the shape of the button?
To completely change the UI for the element we have to use Template feature in Silverlight.
Templating
Template allows to completely customize/change the look and feel of the controls. It allows developers and designers to sculpt the UI of controls in both subtle and dramatic ways, and enables a tremendous amount of flexibility to create exactly the user experience desired.
Template could be created as a standalone resource (with same scope definitions like the styles) or templates could be created as a part of style for the control.
The styles are the recommended location to create templates.
Templates created by defining “ControlTemplate” element with XAML. Each template must define its “TargetType” (exactly like styles). In case of template definition as a standalone resource (not within a style) it also should have key defined within “x:Key” property.
Let’s define simple template for a button:
<ControlTemplate TargetType="Button" x:Key="roundButtonTemplate">
<Grid>
<Ellipse Width="200" Height="200">
<Ellipse.Fill>
<RadialGradientBrush
GradientOrigin="0.2,0.2">
<GradientStop Offset="0.2"
Color="White"/>
<GradientStop Offset="1"
Color="Red"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<TextBlock Text="Close" FontSize="28"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
To apply the template on button use markup extension syntax (exactly like we did for styles):
<Button Content="Hide Me" Width="100" Height="50" FontSize="15" Template="{StaticResource roundButtonTemplate}"/>

Well, the button is not exactly like planned – the text is “Close” (as defined in template) and not “Hide Me” (as defined in the control itself), the size of ellipse is also seems to be wrong. This behavior is because we didn’t specify that the values for those attributes should be taken from the actual control the style being applied on.
In case of temple we could/should specify, that values for the attributes should be taken from the actual control. To do it we will use additional markup extension – “TemplateBinding”. Also, in a case of button (which is a Content Control), the text should be displayed as a content and not as a TextBlock. Let’s rewrite the ControlTemplate.
</pre>
<ControlTemplate TargetType="Button"
x:Key="fixedRoundButtonTemplate">
<Grid>
<Ellipse Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<Ellipse.Fill>
<RadialGradientBrush
GradientOrigin="0.2,0.2">
<GradientStop Offset="0.2"
Color="White"/>
<GradientStop Offset="1"
Color="Green"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
When applied to the button
<Button Content="Exit" Width="100" Height="50" FontSize="15" Template="{StaticResource fixedRoundButtonTemplate}"/>

The button looks like expected.
The ControlTemplate definition as a part of a style should be in a value of Template property.
Sample (partial) style definition:
<Style x:Key="buttonStyle" TargetType="Button">
<Setter Property="Padding" Value="4" />
<Setter Property="FontFamily" Value="Trebuchet MS"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
...
Then, applying the “buttonStyle” on the button will change the Button’s properties and the button template according to defined in style.
When utilizing Control Templates, the templated control still has all the functionality of the original control (in the case of button it is click, mouse hover, etc.)
Conclusion
The styling and templating are a very powerful feature, which allows to completely customize standard look and feel for any custom control.
Tutorials