views:

392

answers:

3

Anyone found a good pattern for getting around immutable styles in Silverlight 2?

What I mean is does anyone have a workaround for the fact that you cannot switch the style of an element programmatically once it has been set, i.e. the second line here will throw a catastrophic failure exception:

this.TestButton.Style = (Style)Application.Current.Resources["Fred"];
this.TestButton.Style = (Style)Application.Current.Resources["Barney"];

A: 

I don't know if this helps, but I believe you can change the control's template as many times as you want during runtime. Maybe that would be a potential workaround.

Page Brooks
+2  A: 

It's not possible. The best workaround I've seen is from Nikhil Kothari at Microsoft:

http://www.nikhilk.net/Silverlight-Themes.aspx

There is a major drawback to defining styles centrally in App.xaml anyway, which is that it breaks all designer support when you reference those styles from other user controls. I haven't used it but Nikhil's theme engine looks very promising, and I have a funny feeling that many of his ideas will make it into the silverlight product eventually anyway.

Pete
Gordon Mackie JoanMiro
+2  A: 

The problem goes away in Silverlight 3 where styles are mutable - yay!

Gordon Mackie JoanMiro