Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Countdown to Silverlight 3 #8: Style inheritance

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: