Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Silverlight 3 – BasedOn Styles

Pete Brown - 18 March 2009

Silverlight 3 now includes Based-on styles for cascading/inheriting styles. You’ll find this extremely useful when you need to create, say, several different buttons that share the same control template and several style properties, but change a minor detail like the background and foreground color.

More generically, you can also use this to standardize fonts and colors throughout an application.

To create a based-on style, you simply need to include a reference to the base style in the derived style’s definition:

BasedOn="{StaticResource BaseStyle}"

Now you’ll need to watch yourself when setting BasedOn styles. One might desire to have a standard font for all labels, buttons etc. for example. However, TextBlock inherits from FrameworkElement and re-implements the Font and foreground properties while the other controls inherit from Control. To target them both, you’ll still need to create at least two types of styles. An alternative would be to use a control that does base on what you want, like Label or FieldLabel.

Here is a simple example of how one might set up a hierarchy of styles:

<!-- Base style for all controls -->
<Style x:Key="BaseStyle"
    <Setter Property="FontSize"
            Value="14" />
    <Setter Property="FontFamily"
            Value="Verdana" />
    <Setter Property="Foreground"
            Value="DarkBlue" />

<!-- Base style for all Content controls -->
<Style x:Key="ContentControlBaseStyle"
       BasedOn="{StaticResource BaseStyle}">
    <Setter Property="HorizontalContentAlignment"
            Value="Center" />
    <Setter Property="VerticalContentAlignment"
            Value="Center" />

<!-- Field caption style, based on ContentControlBaseStyle -->
<Style x:Key="LabelStyle"
       BasedOn="{StaticResource ContentControlBaseStyle}">
    <Setter Property="Margin"
            Value="8" />

<!-- Button style, based on ContentControlBaseStyle -->
<Style x:Key="ButtonStyle"
       BasedOn="{StaticResource ContentControlBaseStyle}">
    <Setter Property="Background"
            Value="Orange" />
    <Setter Property="Margin"
            Value="4" />
    <Setter Property="Width"
            Value="150" />
    <Setter Property="Height"
            Value="50" />

<!-- TextBox style, based on BaseStyle -->
<Style x:Key="TextBoxStyle"
       BasedOn="{StaticResource BaseStyle}">

    <Setter Property="Background"
            Value="Yellow" />
    <Setter Property="Margin"
            Value="4" />


<StackPanel Orientation="Vertical" Width="250">
    <controls:Label Content="Label with no style"/>
    <Button Content="Button with No Style" />
    <TextBox Text="TextBox with No Style" />

    <controls:Label Content="Label with new BasedOnStyle"
                    Style="{StaticResource LabelStyle}" />
    <Button Content="Button with new BasedOn Style"
            Style="{StaticResource ButtonStyle}" />
    <TextBox Text="TextBox with new BasedOn Style"
            Style="{StaticResource TextBoxStyle}" />


The resulting screen looks like this:


Combine this with the new merged resource dictionary feature and you’ll find you have a lot less work to do to style an application than you had in Silverlight 2.

posted by Pete Brown on Wednesday, March 18, 2009
filed under:    

Comment on this Post

Remember me

3 trackbacks for “Silverlight 3 – BasedOn Styles”

  1. DotNetShoutoutsays:
    Thank you for submitting this cool story - Trackback from DotNetShoutout