Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Silverlight - Programatically Re-using the Same Style

0.00/5 (No votes)
19 Jun 2010 2  
What to do if you want to resue a style for more than one component of a particular type on a given Silverlight Page
Today, I was working out some details regarding the use of the Silverlight Data Visualization Toolkit, and came across something I didn't anticipate.

Part of my task includes abstracting out the small details where implementing a chart in our application is concerned. This calls for writing code/XAML to extend/modify the appearance of the charts.

The first thing I wanted to do was make sure I was familiar with the method we need to change colors for a given series. As long as you use the default style for the data points, it's a simple matter of creating a new Style object and adding an appropriate Setter object, like so:

C#
Style  style  = new Style(typeof(ColumnDataPoint));
int index = -1;
Setter setBackground = new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Blue));
style.Setters.Add(setBackground);
series.DataPointStyle = style;

However, I wanted to add DataPoint annotations to the columns, and a customized tooltip, so I created a custom Style in the XAML. Then, I tried to do this:

C#
Style columnStyle = this.Resources["CustomStyle"] as Style;
Setter setBackground = new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Blue));
columnStyle.Setters.Add(setBackground);
series.DataPointStyle = columnStyle;

I was adding multiple series to the chart, and the code above resulted in the FIRST series being displayed correctly, but subsequent series being displayed with the default style (not the right color and no annotations). An exception was being thrown on the line where I was adding the Setter object to the Setters collection. The more astute reader may already see why, but I'll explain it anyway.

When you implement a Style in the XAML, that's the same thing as performing a new in the C# code. In other words, the object is instantiated. What you can't do is add a duplicate Setter object to a Style (if you try set Background twice in XAML, the XAML complains as well). It throws an exception and otherwise causes extensive gnashing of teeth and obscenities directed at your monitor. Once I figured that part out, the fix was an easy one:

C#
Style columnStyle  = new Style(typeof(ColumnDataPoint));
columnStyle.BasedOn = this.Resources["CustomStyle"] as Style;
Setter setBackground = new Setter(ColumnDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Blue));
columnStyle.Setters.Add(setBackground);
series.DataPointStyle = columnStyle;

Simply create a new Style object, and set the BasedOn property to the existing custom Style. After you do that, you're gold. I was then able to set the colors for as many series as I needed.

The tip here is remember the fundamentals, and don't make assumptions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here