views:

2735

answers:

6

Can I use generic.xaml in Silverlight to set the style of all TextBlock in the application?

<ResourceDictionary xmlns="http://schemas.microsoft.com/client/2007"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Style TargetType="TextBlock">
     <Setter Property="Foreground"
       Value="White" />
     <Setter Property="FontSize"
       Value="24" />
    </Style>   
</ResourceDictionary>

I was expecting this to work but it doesn't :-(

Whats the simplest way to apply styles across a whole application??

Thanks, Mark

EDIT

Thanks for your response. I'm not getting an error, the styles just aren't being applied. All the examples that I found on the internet (including the ones you have listed) are all for styling custom controls. This I can do, but I'd like to just style the default controls:

<TextBlock Text="Style me!!" Grid.Row="2"  />

Do I need to add reference to the generic.xaml from page.xaml?? Do I need to name and reference the generic.xaml style as a resource??

Thanks again, Mark

A: 

What exactly doesn't work about it? Do you get an error, or do your text boxes just not get themed?

Your method is the correct way to theme controls, so just work at ironing out the problems you're having with it.

This is a good tutorial, as is this, though note that in Silverlight 2 you need to put the generic file in

themes\generic.xaml

which differs from many tutorials you might find (including the ones given above

Edit: Another tutorial here

Mark Pim
Further information added above as **EDIT**
Mark Cooper
A: 

Try something along the lines of.

<UserControl.Resources>
    <Style TargetType="TextBlock" x:Name="tbStyle">        
        <Setter Property="Foreground" Value="White" />        
        <Setter Property="FontSize"  Value="24" />    
    </Style> 
</UserControl.Resources>

Then when using your TextBlock.

<TextBlock Style="{StaticResource tbStyle}" />
sipwiz
He's asking for application wide control styles, you're scoping it to the individual UserControl...
TreeUK
Yep, this solution isn't scalable enough for our application. We have many projects, which all need a common theme. Hiving resources into a resource dictionary allows us to control these from one location.
Mark Cooper
+5  A: 

Setting Styles generically/automatically by setting the TargetType and omitting a ResourceKey only works in WPF, not Silverlight.

If you want to have a Style available throughout your application you can add it to the Resources collection in your App.xaml, but to use it it will need a x:Key and you will have to apply it individually as a StaticResource binding to each instance of your target type that you want to use it.

Gordon Mackie JoanMiro
Agreed. This is the way and the truth.
TreeUK
I have used the Themeing XAML's from the SL toolkit, and this does what we need, and also decends the control tree.
Mark Cooper
+1  A: 

I do believe this is in Silverlight 3. But the other guys are right, you need to style each one by hand in Silverlight 2.

Things are no different in SL3 - although inheritance from one style to another (using the BasedOn attribute) and mutable styles have been introduced, implicit styling is still only achievable using ISM.
Gordon Mackie JoanMiro
ISM is the way we have gone with this. Using a resource XAML, for example the SL toolkit themes.
Mark Cooper
+1  A: 

Note that you could also use the ImplicitStyleManager from the Silverlight Toolkit to do this:

http://www.codeplex.com/Silverlight/Wiki/View.aspx?title=Silverlight%20Toolkit%20Overview%20Part%203&amp;referringTitle=Home

Apply the style to the top level control in your hierarchy and it will propagate down to all child controls.

James Cadd
+3  A: 

In Silverlight 4, you can use implicit styling!

gius