Page view counter

What’s New In SL3 – Based-on Styles

In Silverlight 3 it is now possible to build a new style based on an existing style, thus reducing duplication of code and creating easier to manage resources (see also Merged Resource Dictionaries).

Thus you could create a style for your buttons that looks like this:

   1: <Style x:Key="StandardButton"
   2:         TargetType="Button">
   3:    <Setter Property="Width"
   4:            Value="100" />
   5:    <Setter Property="Height"
   6:            Value="35" />
   7:    <Setter Property="HorizontalAlignment"
   8:            Value="Left" />
   9:    <Setter Property="VerticalAlignment"
  10:            Value="Bottom" />
  11:  </Style>

You may then create any number of buttons in Xaml using that style,

   1: <Button x:Name="Button1"
   2:         Content="Standard"
   3:         Grid.Row="0"
   4:         Grid.Column="1"
   5:         Style="{StaticResource StandardButton}" />

The new capability of creating a style BASED ON the first allows you to add just those characteristics that you would change, here are two such modifications, the second building on the first,

   1: <!-- Big Button -->
   2: <Style x:Key="BigButton"
   3:       BasedOn="{StaticResource StandardButton}"
   4:       TargetType="Button">
   5:    <Setter Property="Width"
   6:          Value="150" />
   7:    <Setter Property="Height"
   8:          Value="50" />
   9: </Style>
  10:  
  11: <!--BigFont Button-->
  12: <Style x:Key="BigFontButton"
  13:     TargetType="Button"
  14:     BasedOn="{StaticResource BigButton}">
  15:    <Setter Property="FontFamily"
  16:        Value="Georgia" />
  17:    <Setter Property="FontSize"
  18:        Value="24" />
  19: </Style>

 

In this case, the first style, BigButton, adds a new width and height to the standard button, and the second style, BigFontButton, is based on BigButton and adds just a change in font family and font size.  You use the third style just as you would the first, without any need to reference the “ancestor” styles,

   1: <Button x:Name="Button3"
   2:         Content="Big Font"
   3:         Grid.Row="2"
   4:         Grid.Column="1"
   5:         Style="{StaticResource BigFontButton}" />

 

The result is two buttons, one utilizing the original style, the other the twice modified style,

based-on styles.TwoButtons


PreviousPerspective 3d Transform        Next: Data Validation    

Please note that I’m transitioning What’s New In Silverlight 3 from a stand alone wikiDocument to a set of interconnected blog posts to make it more available throughout our various web sites.
Published Saturday, July 18, 2009 1:21 PM by jesseliberty

Comments

# New in Silverlight 3: Perspective Transforms - Jesse Liberty - Silverlight Geek

Pingback from  New in Silverlight 3: Perspective Transforms - Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3 - Validation

Data Validation is fully implemented in Silverlight 3, utilizing the object the input control is bound

Saturday, July 18, 2009 2:05 PM by Jesse Liberty - Silverlight Geek

# What’s New In Silverlight 3 - Validation

Data Validation is fully implemented in Silverlight 3, utilizing the object the input control is bound

Saturday, July 18, 2009 2:17 PM by Microsoft Weblogs

# New and Notable 344

Silverlight 3 Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 8: WCF Based Data Source Building Silverlight 2 and Silverlight 3 from Visual Studio 2008 New in Silverlight 3: Perspective Transforms What's New in SL3

# What’s New In SL3 – Based-on Styles - Jesse Liberty - Silverlight Geek

Thank you for submitting this cool story - Trackback from NewsPeeps

Saturday, July 18, 2009 8:33 PM by NewsPeeps

# re: What’s New In SL3 – Based-on Styles

How would you use the BasedOn property with the default style for a control? That is, can you base a style on the SL standard for a control?

Sunday, July 19, 2009 6:20 AM by snelldl

# re: What’s New In SL3 – Based-on Styles

Hi, at http://www.xamltemplates.net/ you can find themes for all the WPF and Silverlight controls.

Monday, July 20, 2009 4:03 AM by bradutz01

# New in Silverlight 3: Perspective Transforms

3d Perspective Transform In addition to the transforms that were available in Silverlight 2 (translate

Monday, July 20, 2009 1:47 PM by Microsoft Weblogs

# WNISL3-TOC

What's New In Silverlight 3 - Table of Contents 3d Perspective Transform Based on Styles Capture Video

Monday, July 20, 2009 4:00 PM by Jesse Liberty - Silverlight Geek

# What’s New In SL3 – Based-on Styles - Jesse Liberty - Silverlight Geek

9efish.感谢你的文章 - Trackback from 9eFish

Tuesday, July 21, 2009 10:14 PM by 9eFish

# ??????????????? 3??? ????????? ??????(??????) &laquo; jin_u as blog

Pingback from  ??????????????? 3??? ????????? ??????(??????) &laquo; jin_u as blog

# Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in SL 3?

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Was ist neu in SL 3?