Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #55 – How to Apply Styles in Silverlight – Part I

Styles are extremely important because they allow developers to control the look and layout of controls across their application. By using styles you can simply change the style declaration and all controls in the application are updated automatically. This is a much faster alternative to updating each and every control in your application individually.

For this tip we will be exploring the basic fundamental use of styles in Silverlight. I will follow up with more complex scenarios in the next Tip of the Day.

In Silverlight, styles are declared within the controls <UserControl.Resources> tags. If you want to make them global to all controls you can put them in your App.XAML file within the <Application.Resources> tags.

To illustrate this point let’s create a global style on how we will display TextBlocks throughout our sample application:

<Application xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml 
             x:Class="MyApp.App">
    <Application.Resources>
        <Style x:Name="TextBoxStyle" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Calibri.ttf#Calibri"></Setter>
            <Setter Property="FontSize" Value="16"></Setter>
        </Style>
    </Application.Resources>
</Application>

For the XAML above:

  1. Style - Declare each style within <Style></Style>tags in the <Application.Resources> or <UserControl.Resources> section.
  2. Name – Set the name of the style (via x:Name=””) so we can reference it from our controls.
  3. TargetType -The name of the FrameworkElement you are targeting. In our case above we are targeting a “TextBlock”.
  4. Setter - Each property we want to set for this style is declared as a Setter. You can set multiple setters per style. For example, FontSize, FontFamily, etc.

Next, the following XAML below shows how to create multiple TextBlocks with the same style applied to it.

<TextBlock  x:Name="myTextBlock" Text="Hello There." Style="{StaticResource TextBoxStyle}"></TextBlock>

<TextBlock  x:Name="myTextBlock2" Text="How are you?" Style="{StaticResource TextBoxStyle}"></TextBlock>

<TextBlock  x:Name="myTextBlock2" Text="Good thanks!" Style="{StaticResource TextBoxStyle}"></TextBlock>

Finally, if we want to change the font size for these TextBlocks all we have to do is change it in the style setter rather than in each individual TextBlock.

Before: <Setter Property="FontSize" Value="16"></Setter>

After: <Setter Property="FontSize" Value="25"></Setter>

  Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Styles are extremely important because they allow developers to control the look and layout of controls

# October 2, 2008 9:42 PM

j23tom said:

unfortunately, at the moment (RC0) storing styles in App.xaml causes error as mentioned in following thread silverlight.net/.../32602.aspx

# October 2, 2008 10:17 PM

2008 October 03 - Links for today « My (almost) Daily Links said:

Pingback from  2008 October 03 - Links for today &laquo; My (almost) Daily Links

# October 3, 2008 4:18 AM

Silverlight news for October 3, 2008 said:

Pingback from  Silverlight news for October 3, 2008

# October 3, 2008 6:53 AM

Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 3, 2008 | Alvin Ashcraft's Morning Dew

# October 3, 2008 9:42 AM

mike.snow said:

j23tom - Yes, you are correct. We have a bug and are investigating for the release. For now, you can move the style to your UserControl instead of App.xaml if you are using RC0. Thanks.

# October 3, 2008 11:53 AM

sudhakar76 said:

Hello Mike,

 I have been looking for an answere for my question for quite a some time. I really apprecite if you can show some light.

Here is my question:

I have few complex XAML data in file for my UserControl. (XAML data is nothing but creats visual for my UserContol)

For example, assume one file is having XAML data to draw Clock1. And another file has XAML data for different Clock called Clock2.

Now i have one property in my UserControl Called "myStyle".

If myStyle Set to 1, i wanted to draw Clock1 and if its Set to 2 i want to draw Clock 2.

(I took only 2 visual for simplicty. Actually i have lot more visual to choose from.)

I tried to put Clock1 and Clock2 as Styles, But later i learned Its not possible to change the Style once its assigned.(Hope i am right)

What is the best way to do this?

There is one possiblity is read XAML data in Code behind and Add each elements of XAML as child to UserControl whenever myStyle changes. Is that the only way to do it?

Or is there any better way to do it using style?

I really appreciate your help.

Thanks.

# October 3, 2008 3:18 PM

Community Blogs said:

In this issue: Denislav Savkov, Martin Mihaylov, Matthias Shapiro, Mike Snow, Terence Tsang, Jesse Liberty

# October 3, 2008 3:48 PM

Mirrored Blogs said:

Post: Approved at: Oct-8-2008 Tip:How to Popup a Browser Window &quot;Let’s say a user clicks on a button

# October 8, 2008 5:50 AM

Visual Web Developer Team Blog said:

Silverlight Tip of the Day #57 Title: How to Dynamically Load a Silverlight Control within another Silverlight

# October 8, 2008 11:05 PM

mike.snow said:

sudhakar76 - You are correct that styles cannot be changed dynamically once set. Hopefully this will be working in the next release.

# October 9, 2008 8:12 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:25 PM

MS Tech News » Silverlight Tips of the Day ??? Week 8 said:

Pingback from  MS Tech News &raquo; Silverlight Tips of the Day ??? Week 8

# October 27, 2008 3:31 PM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:57 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM