DCSIMG
Silverlight 4 Quick Tip: Styling application - Alex Golesh's Blog About Silverlight Development

Silverlight 4 Quick Tip: Styling application

Silverlight 4 supports default styles. Default styles are styles with TargetType, but without x:Key property set. Those styles will be applied to all controls from corresponding type.

The sample of default (also sometime called anonymous) style:

<Style TargetType="Button">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Background" Value="Red"/>
</Style>

This style will be applied on any button in the scope of the style. If this style will be placed in App.xaml it will be applied on all buttuns in application.

Now let’s style the application with different styles – I’ve created 2 sample Resource Dictionaries to be the “Themes” of my application with a number of styles for different controls. My sample “Theme 1”:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Background" Value="Red"/>
    </Style>
    <Style TargetType="TextBox">
        <Setter Property="FontSize" Value="20"/>
    </Style>
    <Style TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Foreground" Value="Blue"/>
    </Style>
</ResourceDictionary>

My sample “Theme 2”:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Margin" Value="5"/>
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="Height" Value="Auto"/>
        <Setter Property="Background" Value="Blue"/>
    </Style>
    <Style TargetType="TextBox">
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="Foreground" Value="Green"/>
        <Setter Property="Background" Value="Yellow"/>
    </Style>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Foreground" Value="Red"/>
    </Style>
</ResourceDictionary>

Now I’ll show how to apply those dictionaries to the application. Sample UI will be like follows:

<StackPanel x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Horizontal">
            <Button Content="Theme 1" x:Name="btnTheme1" Click="btnTheme1_Click"/>
            <Button Content="Theme 2" x:Name="btnTheme2" Click="btnTheme2_Click"/>
            <Button Content="No Theme" x:Name="btnNoTheme" Click="btnNoTheme_Click"/>
        </StackPanel>
        <StackPanel Orientation="Vertical">
            <Button Content="Test"/>
            <TextBlock Text="Some text here..."/>
            <TextBox Text="Some editable text here..."/>
        </StackPanel>
</StackPanel>

image 

On corresponding button event I’ll apply the styles. Both resource dictionaries were added to the project as a Content resources. Let’s see the “Theme 1” button EventHandler:

ResourceDictionary rd = new ResourceDictionary();
//Load resourse dictonary
rd.Source = new Uri("/Theme1.xaml", UriKind.RelativeOrAbsolute);
//Clear previous styles if any...
App.Current.Resources.MergedDictionaries.Clear();
//Add the loaded resource dictionary to the application merged dictionaries
App.Current.Resources.MergedDictionaries.Add(rd);

The second theme is exactly the same. To “Clean” the themes from application just clear MergedDictionaries:

App.Current.Resources.MergedDictionaries.Clear();

Running application:

image

image

Application source here.

 

Stay tuned for more features how-to’s and tips

Alex

Published Wednesday, November 18, 2009 9:24 PM by Alex Golesh

Comments

# Silverlight 4 Quick Tip: Styling application - DevCorner Silverlight Blog

Pingback from  Silverlight 4 Quick Tip: Styling application - DevCorner Silverlight Blog

# re: Silverlight 4 Quick Tip: Styling application

Very good article. Where is the code?

Tuesday, January 12, 2010 10:24 AM by magavnuk

# re: Silverlight 4 Quick Tip: Styling application

Hi,

I have gone through your above sample. It's working fine. But if I given x:key to "Button" in "ResourceDictionary" for your above code it's not working(my code goes like this see below).

In ResourceDictionary:

--------------------------------

<Style x:Key="ButtonStyle" TargetType="Button">

In the MainPage.xaml:

----------------------------------

<Button Content="Theme 1" x:Name="btnTheme1" Style="{StaticResource ButtonStyle}" Click="btnTheme1_Click"/>

but I got this error. Please resolve my problem asap. Please not my personal mail ID: ashok.just4u

ERROR:

----------

Cannot find a Resource with the Name/Key ButtonStyle [Line: 11 Position: 64]

-Ashok N

ashok.just4u@gmail.com

Tuesday, April 27, 2010 6:41 PM by Ashok N

# re: Silverlight 4 Quick Tip: Styling application

not able to apply theme dynamically

Tuesday, January 15, 2013 2:05 AM by Vaishnavi

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: