Countdown to Silverlight 3 #8: Style inheritance

by Andrej Tozon 11. July 2009 17:13

Silverlight 3 supports style inheritance through setting the BasedOn property. Although not as powerful as that in WPF, using this feature can significantly improve the maintainability of application styles – instead of duplicating several properties when creating similar styles, you can define one base style, set some common properties, then create a a derived style, inheriting those properties and adding new ones.

When defining a new style, the BasedOn property an be used to reference a base style, thus extending it with new properties. All you have to be careful about is to target the same type (or its derivate) as the base style. For example, you can’t create a new style, targeting a Button, if the base style targets a TextBox. You can, however, target a Button, if the base style targets a Button, ButtonBase, ContentControl, etc (ascendants of a Button). You can also overwrite property values, set in a base style – either in a derived style, or by setting it directly on a control.

The example below covers some of the mentioned scenarios.

Style inheritance

Run the application online

Source code below:

Tags:

Development | Silverlight

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

View Andrej Tozon's profile on LinkedIn

Subscribe to me on FriendFeed

Andrej Tozon's Facebook profile

Get help from Andrej Tozon!

RecentComments

Comment RSS