Latest Tweet:
  • Loading...

This blog post is one of three I’m posting to introducing some of the new features in the Silverlight Toolkit announced today at the PDC 2008. For more information about and downloads of the toolkit check out http://www.codeplex.com/Silverlight.

One of the strengths of WPF and Silverlight is the ability to change the style or template of any control. This gives the designer maximum flexibility to change the look and feel of an application, without breaking its behavior. The Silverlight 2 styling support has some major shortcomings, specifically around dynamic and implicit styles. Dynamic styling enables you to change the style of a control at runtime without having to recreate it. Implicit styling means applying styles to all controls based on type, and not a specific style name. Implicit styling is similar to applying CSS styles to a H1-element, instead of having to specify the CSS-class name of the element. The benefit of implicit styling is that you can change the look and feel of all the buttons, textboxes, checkboxes or any other control without having to apply a specific style key to all the elements. The Silverlight Toolkit introduces the ImplicitStyleManager which adds support for implicit styling and themes to Silverlight 2.

There are already quite a few good looking themes for Silverlight 2. Tim Heuer did a blog post earlier this month covering some of the themes available, and specifically the work of Corrina Barber and Dave Crawford comes to mind as great examples. Today these templates are deployed as XAML files, with instructions on which part of the file to copy and paste into your App.xaml file. You also have to apply the styles to every single control in your application, and the name of each style is often different. Dave calls his button style BlackGlossyButton, which is different from Corrina’s naming convention. That means that you have to go over and re-apply the style to all your controls if you want to use Corrina’s theme instead of Dave’s theme.

With the new ImplicitStyleManager designers like Corrina and Dave can style a button without having to give it a specific key. They can share their themes as resource dictionary file, and any developer who wants to use the theme simply includes this file and applies it using the ImplicitStyleManager. This is going to make theming of applications allot easier as you simply include a new XAML file to get a new look and feel of your application. Scott Guthrie has commented on his blog that they will be creating some form of online gallery where designers can share their Silverlight themes.

The way create themes is fairly straight forward. You add a new XAML file to your project and mark it as Content (and not as Page which is the default option). Next you build up your resource dictionary, and for your styles you no longer have to apply a key (unless you want to). You simply set the target type to style a button, textbox or any other control. The theme gets applied to your application by using the ImplicitStyleManager attached properties on any container control. All child elements inside that container will get the theme style applied.

ImplicitStyleManagerSample

For this example I have taken Dave’s glossy control theme and turned it into a Silverlight Toolkit theme. When I run the example code above, this is what my button looks like:

ImplicitStyleManagerHelloWorld

All I had to do to turn Dave’s glossy theme into a Silverlight Toolkit theme was remove the key on each on the style. That way I don’t have to specify the key for my button style, as the theme will be applied implicitly by the ImplicitStyleManager. The next screenshot shows a typical data entry screen using Dave’s theme and the Shiny Blue theme from the Silverlight Toolkit (one of the six included themes).

TwoAppliedThemes

In the above example I didn’t have to change any of the XAML of the application, or name of styles, in order to change the entire look and feel of my application. In the future I’m sure we will be seeing lots of new themes from the community as well as commercial vendors. The ImplicitStyleManager makes it easy to share themes and apply them to your application. There are still some shortcomings to the styling model. Support for dynamic resources is one of them. In order to change the theme while the application is running you have to recreate your UI before applying the theme. This is almost like having to “reboot” your user interface, and is not an optimal user experience. However, having Theme support is still a great way forward and opens up many new possibilities.

Wednesday, October 29, 2008 3:41:31 AM (W. Europe Standard Time, UTC+01:00)
let's say I want to offer 5 themes to my user to pick from, and at runtime I can show the selected theme. Does this mean in every page I have to have 5 copies of the same controls in 5 theme containers throughout the entire app, in order to support 5 or even more themes?
If yes, what if these controls have names and there are code behind associated with these names?

Thanks!

..Ben
Wednesday, October 29, 2008 4:38:26 AM (W. Europe Standard Time, UTC+01:00)
how to dynamically shift different themes with easy-to-use a few lines(even one single) of code?
Wednesday, October 29, 2008 10:29:44 AM (W. Europe Standard Time, UTC+01:00)
Uri uri = new Uri(@"~/XAML/ShinyBlue.xaml", UriKind.Relative);
ImplicitStyleManager.SetResourceDictionaryUri(item, uri);
ImplicitStyleManager.SetApplyMode(item, ImplicitStylesApplyMode.Auto);
ImplicitStyleManager.Apply(item);
Everett
Wednesday, October 29, 2008 4:37:32 PM (W. Europe Standard Time, UTC+01:00)
Hi,
Tried this approach:
Uri uri = new Uri(@"~/XAML/ShinyBlue.xaml", UriKind.Relative);
ImplicitStyleManager.SetResourceDictionaryUri(item, uri);
ImplicitStyleManager.SetApplyMode(item, ImplicitStylesApplyMode.Auto);
ImplicitStyleManager.Apply(item);

to dinamically change my theme and it worked, but right after, I tried to apply other theme (ShinyRed.xaml) and it didn't allow me to do so. So sems like once I applied theme, I can't dinamically reapply other one. Is this how it supposed to be, or am I missing something?

Thanks
Eugeniy
Wednesday, October 29, 2008 4:43:12 PM (W. Europe Standard Time, UTC+01:00)
Hi,

Perhaps I wasn't clear enough, but ImplcitStyleManager solves implicit styling, and not dynamic styling. So after you apply a style, you can change it (unlike WPF where this is supported). Hopefully this gets added to a later release of Silverlight.

To enable theme changing you need to re-build your UI. So basically:

1) Remove your User Control / "Stuff" from your container
2) Re-instantiate a new instance of your User Control and add it to your container
3) Apply the new theme to the User Control.

So it's kind of like "re booting" your UI to change theme... So you have to warn the user that they have to save any unsaved work etc.
Wednesday, October 29, 2008 5:24:36 PM (W. Europe Standard Time, UTC+01:00)
you can find a quick sample here
http://leeontech.wordpress.com/2008/10/29/changing-styles-using-implicitstylemanager/
Thursday, October 30, 2008 1:43:39 AM (W. Europe Standard Time, UTC+01:00)
Hi Eugeniy,
You must remove the control Frist, and create it again, just like Jonas said
Everett
Thursday, October 30, 2008 8:49:57 AM (W. Europe Standard Time, UTC+01:00)
recreate the ui is NOT a good idea :)
Thursday, October 30, 2008 11:28:43 PM (W. Europe Standard Time, UTC+01:00)
I keep getting the "AG_BAD_PROPERTY_VALUE" error in the Page.xaml on the ResourceDictionaryUri.

xmlns:theming="clr-namespace:Microsoft.Windows.Controls.Theming;assembly=Microsoft.Windows.Controls.Theming"
theming:ImplicitStyleManager.ApplyMode="OneTime" theming:ImplicitStyleManager.ResourceDictionaryUri="Themes/Theme.xaml"
slprince
Thursday, November 06, 2008 2:26:52 PM (W. Europe Standard Time, UTC+01:00)
Does this means that if I want to have different buttons on single page follow different themes, I will need to create so many XAML files and point to them using ISM, or can a single theme XAML contain multiple definitions for button control? Since there isn't any "Key" identifier used, I really suspect that this will work without splitting into so many XAML files.

Isn't this a maintenance overhead?
Thursday, November 06, 2008 11:36:21 PM (W. Europe Standard Time, UTC+01:00)
@AtulGupta: The benefit of using the ImplicitStyleManager is to style controls without using a key. But if your scenario requires you to have one style for most of your buttons (no key), and one additional style for some buttons (with key), you can absolutely do that.

You can have a single Theme XAML file, and add two button styles, one with a key and one without a key. The ISM will do the right thing and apply the correct style.

Friday, November 07, 2008 9:28:16 PM (W. Europe Standard Time, UTC+01:00)
RE: AtulGupta / Jonas

Could you provide an example of how to reference a named style from a single Theme XAML file? Using an implicit style seems straightforward, but I don't see how to reference a named style from the same theme. Thanks!
David
Friday, November 21, 2008 11:30:31 AM (W. Europe Standard Time, UTC+01:00)
Super cool, I have a web site(http://www.xamltemplates.net) were i sell themes (skins) for all the WPF controls, now i cand make also for silverlight :D, thanks again.
Tuesday, December 02, 2008 7:46:48 PM (W. Europe Standard Time, UTC+01:00)
Hi Jonas, thank you for a very informative blog post.
I have a question: can you see styled controls in VS designer or in Blend?
I used ISM and everything works fine when I run the project, but I can't see it in VS designer/Blend anymore :(
newdrim
Wednesday, December 03, 2008 11:55:26 PM (W. Europe Standard Time, UTC+01:00)
@newdrim: You can't see anything - or you can't see the applied theme? I haven't experimented with the ISM in Blend yet - but if that is the case it is a serious problem... One option could be to apply the theme using code and not markup, and then in your code do a check like this:

if(HtmlPage.IsEnabled)
{
// Apply theme
}

That way the ISM would only be applied when the code is executing in the browser (where things work fine) - and not in Blend (so that you would not break the design experience).
Monday, January 05, 2009 6:25:29 AM (W. Europe Standard Time, UTC+01:00)
Jonas - you said you converted the GlossyBlack theme to work with the ISM, but having followed the steps you've described I'm getting an error - unable to load themes. Any idea why that might be? Is the Xaml you use available?

thanks!
Michael O'Connor
Tuesday, January 06, 2009 12:45:27 PM (W. Europe Standard Time, UTC+01:00)
@Michael: I can't remember exactly what changes I had to make.. I used Blend + Trail & Error to make it work. I've uploaded the file to http://cid-1a08c11c407c0d8e.skydrive.live.com/self.aspx/Code%20samples/DavesGlossyControls.xaml

Cheers,
Jonas
Monday, February 02, 2009 8:41:39 PM (W. Europe Standard Time, UTC+01:00)
Is there any way to load scheme only once per application? I don't want to repeat code on every page.

Thanks
AKa
Monday, February 09, 2009 10:29:38 AM (W. Europe Standard Time, UTC+01:00)
AKa: Depends on your requirements. One way to avoid code duplication is to create a user control base class that does the styling for your.
Wednesday, March 18, 2009 2:44:05 PM (W. Europe Standard Time, UTC+01:00)

Finally, got what I was looking for!! I definitely enjoying every little bit of it. Glad I stumbled into this article! smile I have you bookmarked to check out new stuff you post
Thursday, April 30, 2009 4:55:10 PM (W. Europe Daylight Time, UTC+02:00)
At http://www.xamltemplates.net/sl you can find a Silverlight complete theme for all the controls, and I used ISM for general themeing without keys.
Thursday, July 02, 2009 8:36:02 PM (W. Europe Daylight Time, UTC+02:00)
So dynamic styles aren't supported. This is odd, because you can dynamically change all of the properties that are set by the style definition. So, with a custom themer you can define your own XML definition and use the ImplicitStyleManager VisualTreeHelper and Functional methods to apply style properties dynamically to all your controls.

Unfortunately I can't divulge implementation details, i'm hoping other people can use the idea to develop something open-source. Essentially you loop through the entire visual tree, updating the values of the element's dependency properties based on style and/or element type using your custom XML definition to construct a lookup dictionary.

This would be made simpler if only Style setters held onto their property, for some reason they're all null but the values are still there.
Lowkay
Thursday, October 29, 2009 12:21:44 PM (W. Europe Standard Time, UTC+01:00)
I tried to implement the implicit styling, it does not applies the themes on user controls on the page.
Venk
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview
<March 2010>
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910