(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .
        Login with Facebook

Tip: Cascading Styles in Silverlight 3

(3 votes)
Emil Stoychev
>
Emil Stoychev
Joined Oct 15, 2007
Articles:   23
Comments:   98
More Articles
11 comments   /   posted on Mar 23, 2009
Categories:   Design

 

Like in CSS you can now cascade styles in Silverlight 3 too. You can append and/or override values by basing one style on another. In this way you can built a powerful and reusable set of styles to apply in your Silverlight applications.

 

Syntax

Style cascading is accomplished by using the new BasedOn attribute of the Style class.

 1: <Style x:Name="Headline" TargetType="TextBlock">
 2:     <Setter Property="FontFamily" Value="Verdana" />
 3:     <Setter Property="FontSize" Value="14" />
 4: </Style>
 5: <Style x:Name="ImportantHeadline" TargetType="TextBlock" 
 6:     BasedOn="{StaticResource Headline}">
 7:     <Setter Property="Foreground" Value="Red" />
 8: </Style>        
 9: <Style x:Name="HomePageHeadline" TargetType="TextBlock" 
 10:     BasedOn="{StaticResource Headline}">
 11:     <Setter Property="FontSize" Value="18" />
 12: </Style>

Here we have a base style Headline that targets TextBlock elements and defines values for FontFamily and FontSize. Next we have another style ImportantHeadline which again targets TextBlock elements. Note here the additional attribute BasedOn(Line 6). By using it we are basing this style on the style defined by its value – Headline in this example. Here we set the Foreground property of the TextBlock to be Red.

When we use the ImportantHeadline style on a TextBlock we are going to have both of the values set in the Headline style(FontFamily, FontSize) and those set in ImportantHeadline(Foreground).

Then another style HomePageHeadline is defined. It is also based on the Headline style, but this time it overrides the base value set for FontSize. Now when we use the HomePageHeadline on a TextBlock we can see that it appears  with FontFamily Verdana and FontSize 18.

The results are as follows:

Download Source Code

Note: you can cascade styles on more than one level. Following the example above you can base another style on the HomePageHeadline and you will have 3 levels of cascading.

Hope that helps!

That's it!


Subscribe

Comments

  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Vince on Apr 30, 2009 08:17

    Nice artcile. Thankfully Silverlight is catching up with WPF!

    Do you know how to derive a framework control's Style like TabControl in a derived MyTabControl while defining the style for MyTabControl in a generic.xaml? It seems to work automatically in an app.xaml, but not in a generic.xaml.

  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Alexey Zakharov on Jul 09, 2009 21:04
    Cool. I dreamed about it in SL2 =)
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Bts on Oct 12, 2009 09:11
    Thanks!
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by DWF on Apr 08, 2010 20:10
    This is not helpful. Why doesn't it show us where to put the style code on the page? IE Where does the style get declared and how is it assigned to a control.
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by gaurav on May 11, 2010 12:07
    Thanks.....its very helpful
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by az on Jun 08, 2010 07:44
    is it possible to maintain a separate xaml file for all the styles?  and use them globally within the project?
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Ali on Jun 18, 2010 16:17
    Yes - put them in the application xaml
  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Kieran on Sep 19, 2010 15:15

    Is it possible to derive from anonymous style? e.g.

            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="5"/>
            </Style>

  • emil

    RE: Tip: Cascading Styles in Silverlight 3


    posted by emil on Sep 20, 2010 16:40

    No, Kieran, that's not supported.

  • -_-

    RE: Tip: Cascading Styles in Silverlight 3


    posted by Manuel on Jan 07, 2011 22:35

    Is posible to derive from a style in another custom dll. For example:

    mydll.dll

    <Style x:key="StyleA" TargetType="TextBlock">
                <Setter Property="Margin" Value="5"/>
            </Style>

     

    another.dll

    <Style x:key="StyleB" TargetType="TextBlock" BasedOn="{StaticResource StyleA}">
                <Setter Property="Margin" Value="5"/>
            </Style>

     

    thanks

  • emil

    RE: Tip: Cascading Styles in Silverlight 3


    posted by emil on Jan 10, 2011 10:10

    Hi Manuel,

    Yes, you can. Use a merged resource dictionary to merge the styles from the mydll.dll with the ones in another.dll. Here is an article on how to use merged resource dictionaries - http://www.silverlightshow.net/items/Merged-Resource-Dictionaries-in-Silverlight-3.aspx

    Emil

Add Comment

Login to comment:
  *      *       
Login with Facebook