Styles

Manipulating Styles At Runtime in Silverlight

I’ve been writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were Silverlight Styles: an Introduction, Silverlight Styles: Advanced Topics and Silverlight Styles: Further Advanced Topics

The final part of the series is ready and it covers the following topics:

  • Modifying Styles Runtime: the IsSealed Property – modifying the Setters of a Style can only be done if the IsSealed property is set to false and this property is set to true as soon as the style gets applied to an element in the visual tree.
  • Cloning Styles – because sealed Styles can’t be modified, one needs to clone them to be able to manipulate them. I’m providing example code on how cloning of a Style can be done.
  • Merging styles runtime – I’m showing two ways of merging existing styles programatically, both using the BasedOn property of Style as well as just copying Style setters.

You can read the detailed article and download source code for examples on my Visiblox blog – I hope you’ll find it useful.

Disappearing Style Setters in Silverlight 3 and WP7 – fixed in Silverlight 4

I’ve come across a really annoying bug in Silverlight 3 that seems to be fixed in Silverlight 4, but still present in WP7. When creating a Style object in runtime, then setting that as the Style of an element in the visual tree, the Value properties in the Setters of the original Style are set to null!

The problem

I’ve come across this issue because I was trying to clone styles with the following extension method:

public static Style Clone(this Style style)
{
    if (style == null)
        return null;
    Style clonedStyle = new Style(style.TargetType);
    clonedStyle.BasedOn = style.BasedOn;
    foreach (Setter setterToCopy in style.Setters)
    {
        clonedStyle.Setters.Add(new Setter()
        {
            Property = setterToCopy.Property,
            Value = setterToCopy.Value
        });
    }
    return clonedStyle;
}

As you can see, this clone method is not doing anything complicated: it is simply iterating through the Setters of the Style and creating new Setter instances with the Property and Value of the original Setters – basically deep cloning the Style itself.

The issue in Silverlight 3

When I did clone a Style that had been created on the fly and had been assigned a the Style of an element, the values of Setters magically were set to null in Silverlight 3. The problem is visualized using the following simple code:
Read More…

Styles in Silveright – Data Binding, Runtime Declaration and Further Advanced Topics

I’m writing a 4 part series on everything (I think) is worth knowing about styles in Silverlight. The previous parts were Silverlight Styles: an Introduction and Silverlight Styles: Advanced Topics.

The third part of the series is ready and it covers the following topics:

You can read the detailed article on my Scott Logic blog – I hope you’ll find it useful.

The final part of the series will explore manipulating styles in runtime.

Styles in Silverlight – Advanced Topics

I’m writing a 4 part series on everything (I think) it’s worth knowing about styles. In the second part of the series (following the previous one Styles in Silverlight – an Introduction) I’m covering the following 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.
    So for example the Fill property of both an Ellipse and Rectangle can be styled by using a Style that’s TargetType is set to Shape (as both Ellipse and Rectangle inherit from Shape)
  • Inheriting Styles using the BasedOn property: Styles can be inherited from one another using the BasedOn property. Single inheritance is supported and the depth of inheritance is not limited.
  • Implicit styling: Implicit styles allow defining a default style for all instances of a given type within the scope of the control. This feature has been introduced in Silverlight 4.
  • Style Precedence: Styles are only applied to a property if that property is not animated, has no local value set and has no value set in a template. This precedence is the same as the dependency property precedence.
  • 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.
  • You can read the (much more detailed) full article on my Scott Logic blog. The next part of the series will cover further advanced topics such as using Styles in code behind and exploring the relationship between styles and dependency properties from a practical perspective.

Styles in Silverlight: an Introduction

I’m writing a (planned) 4 parts series on everything I think it’s worth knowing about styles in Silverlight. The first part is ready and it covers the following topics:

You can read the (much more detailed) full article on my Scott Logic blog. This part has meant to be an introduction, the next parts will dig into more advanced Style usage scenarios.

 Scroll to top