This post is a summary on my 4 part series covering everything I
think a developer needs to know about Styles in Silverlight in
order to work with them in XAML or programatically. The overview of
the series is as follows. All of the topics link to the original,
more detailed article: feel free to explore the parts that interest
Inheritance, Precedence and Other Advanced Topics
Re-Using Styles for Different Types: the TargetType of the
Style can be a parent type of the target object as long as it only
sets properties of the parent type.
Style Inheritance: The BasedOn property - inheriting styles can
be done using the BasedOn property. Styles support single
inheritance and the depth of inheritance is not limited.
Implicit Styling - in Silverlight 4 the default styles of
elements on a page / control can be specified in a simple way using
Style Precedence: a Style's Setters only get applied if the
object's particular property is not animated, not set locally and
not set in the control template belonging to the object.
Style Setter Precedence: setters specifying the value of the
same property may be declared within the same Style. The last one
of them has the highest precedence and will be used.
Further Advanced Topics
Style is a Dependency Property. This means that it supports the
Data binding: it's possible to bind styles on to another - even
though this is probably a very rare real world scenario.
Property changed notifications: it's not possible to directly
subscribe to property changed notifications for the Style property
due to the feature missing in Silverlight, however there is a
complicated, but working workaround available to detect when the
Style is being changed.
ClearValue: unsetting the value of a Style is not the same as
setting it to null. When wanting to reset or clear the style,
ClearValue is the method to be used.
Declaring Styles In Code Behind is possible, it results in more
complex code though then doing the same thing in XAML.
Other Notes About Styles
- Only Dependency
Properties Can Be Styled: styling CLR properties will result in
either a compile time or runtime error.
Not All Classes Have Styles: only Ones Derived From
FrameworkElement have the Style property and thus can be styled.
All of the built-in visual controls, the Control and UserControl
class are descendants of this class. It's worth remembering this
when creating custom classes that might need to be styled.
Manipulating Styles at Runtime
I hope this summary on styles in Silverlight was helpful.
Interested in an easy to style, very performant Silverlight / WPF
charting library? Give the free version of