Make a theme-changable application

This is how I do to make the real application theme-changable

1. Set up an independent project of WPF User Control Libaray. Remove the default generated user control

2. Create a folder called Themes. Under this folder, add bunch of ResourceDictionaris.

3. Open up each of the ResourceDictionaris and edit the styles and whatever necessary.

4. Compile the project to dll, and Done!

To link the theme dll, you add the preference to the dll into your theme-consuming project. You can consume the themes by either merging them in Application.Resources or loading them dynamically.

To merge a theme, you put the following XAML code into the Resource section of App.xaml

<ResourceDictionary Source=”/ThemesAssemblyName;component/Themes/ATheme.xaml” x:Name=”theme”/>
<ResourceDictionary Source=”OtherTheme.xaml”/>

To load a theme dynamically, such procedural code is required

ResourceDictionary theme = Application.LoadComponent(new Uri(“Themes/AnotherTheme/AnotherTheme.xaml”, UriKind.Relative)) as ResourceDictionary;
Collection<ResourceDictionary> mergedDicts = Application.Current.Resources.MergedDictionaries;
ResourceDictionary oldtheme = Application.Current.Resources.FindName(“theme”) as ResourceDictionary;

If you have the themes in the same assembly with your applicaiton, you should change the Uri to “Themes/ATheme.xaml”

Note: (Kinda off-topic, BUT!!!) when you define the control template for some control, no matter the control template is inline or wrapped by Style, the property of TargetType is required, otherwise, the Contentpresenter of that control will not work.

Note1: Use DynamicResource if you want the resource consumer can be dynamically rendered when a new theme is applied. Another thing is, use DynamicResource if the resource consumer in a data/control template, otherwise, the project will not be blendable.

Some sample links:

WPF: Expression Blend 2 and external resources (VS2008 version)
Create and apply custom themes
Creating a Skinned User Interface in WPF


~ by Martin on September 28, 2007.

2 Responses to “Make a theme-changable application”

  1. At you can see themes and there is one for free you can download and use. It has all the controls stylized.

  2. good app

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: