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”/>
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: