Subscribe to our RSS Feeds

Welcome to Silverlight Blog!

Hello Visitors !

You found a Blog about Microsoft Silverlight by Sascha Baumann,
Freelance Software Developer based Berlin, Germany.

If you need development, coaching or a
second opinion - Just contact me.

Have fun looking around :o)

Sascha Baumann

Styles from different files as static resources

No Comments »

Hello everyone,

to start a silverlight project, i have a kind of default-template which I enriched with a CSS-like decentralized style today. This is done very easy now in Silverlight 3. First you habe to set up a file containing your style. In my case it was a file in a new Styles folder called TextBock.Default.xaml where I entered this dummy code:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Windows="clr-namespace:System.Windows;assembly=System.Windows">
    <Windows:Style x:Key="TextBlock.Default" TargetType="TextBlock">
        <Windows:Setter Property="FontFamily" Value="Times New Roman"/>
        <Windows:Setter Property="FontSize" Value="16"/>
        <Windows:Setter Property="Foreground" Value="Black"/>
        <Windows:Setter Property="LineHeight" Value="26"/>
    </Windows:Style>
</ResourceDictionary>

after that, I created a MergedDictionary in my app.xaml like that:

 <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="Styles/TextBlock.Default.xaml"/>
             </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
     </Application.Resources>

from now on, you can call this style like you’re used to with {StaticResource TextBlock.Default} within your application. Nice way to manage the styles as I think, and much easier to maintain than having all your styles plugged in your app.xaml in total.

Have fun playing with it :o )
Sascha

Various Oktober 27th 2009