Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Silverlight - Programatically Re-using the Same Style

, 19 Jun 2010
Rate this:
Please Sign up or sign in to vote.
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:
 
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:
 
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:
 
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralReason for my vote of 4 good Pinmemberqinquan11-Apr-11 20:13 
GeneralReason for my vote of 5 It works... Thanks a lot. Pinmemberjkkumar21059-Aug-10 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 19 Jun 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid