Setting the StrokeDashArray using a Style in Silverlight

I’ve recently ran into an issue: I wanted to draw an element with a dashed stroke using a Style I’ve created on the fly. To my surprise I couldn’t.

The Problem

The type of the StrokeDashArray (that is defined on Shape) is a DoubleCollection. So naturally I tried setting a DoubleCollection instance as the Value of the Setter on the Style I created:

Style ellipse2Style = new Style(typeof(Ellipse));
// Setting a DoubleCollection as the Value of the Setter for StrokeDashArrayProperty doesn't work!
ellipse2Style.Setters.Add(new Setter(Ellipse.StrokeDashArrayProperty,new DoubleCollection() { 2, 2 }));
Ellipse2.Style = ellipse2Style;

This is interesting enough as setting the StrokeDashArray property directly seems to work just fine:

// Works fine
Ellipse1.StrokeDashArray = new DoubleCollection() { 2, 2 };

This issue definitely suggests that this is a bug that is still present in Silverlight 4. A little searching revealed that this issue has been reported in version 2 (almost 2 years ago!), but is still not fixed.

The Workaround

After some playing around I’ve found a workaround: the Setter property needs to be passed as a string, rather then a DoubleCollection.

The fact that this helps solve the issue is quite surprising as it suggests that there is some logic built in to the styling mechanism that in case of the StrokeDashArray property does some funky conversion from string to DoubleCollection. Personally I would’ve thought that only the XAML parser would do something similar, but it looks like this isn’t a case.

So the (strange) workaround is to set the value of the Setter for the StrokeDashArrayProperty to a string, just like one would do when declaring the Style in XAML. The above example can be made working using this code:

Style ellipse3Style = new Style(typeof(Ellipse));
// Setting a string as the Value of the Setter for StrokeDashArrayProperty works - odd!
ellipse3Style.Setters.Add(new Setter(Ellipse.StrokeDashArrayProperty, "2,2"));
Ellipse3.Style = ellipse3Style;

As for a demo of the bug, see the following application:

The code behind for the application is as follows:

// Works fine
Ellipse1.StrokeDashArray = new DoubleCollection() { 2, 2 };
 
Style ellipse2Style = new Style(typeof(Ellipse));
// Setting a DoubleCollection as the Value of the Setter for StrokeDashArrayProperty doesn't work!
ellipse2Style.Setters.Add(new Setter(Ellipse.StrokeDashArrayProperty,new DoubleCollection() { 2, 2 }));
Ellipse2.Style = ellipse2Style;
 
Style ellipse3Style = new Style(typeof(Ellipse));
// Setting a string as the Value of the Setter for StrokeDashArrayProperty works - odd!
ellipse3Style.Setters.Add(new Setter(Ellipse.StrokeDashArrayProperty, "2,2"));
Ellipse3.Style = ellipse3Style;

Download the source of the example here: StrokeDashArray Bug in Silverlight.zip

Hope this helps anyone coming across this issue!

2 Responses to Setting the StrokeDashArray using a Style in Silverlight
  1. You should check out this link I have posted to your trackback... Did you request that I lin... dotnetshoutout.com/interoir-design-courses-online-2