Hallelujah.
I've been trying to force all our XAML teams to adopt that theming mechanism ever since Silverlight 3 introduced Implicit Styles but yet to no avail. You see it is a brutal breaking change they say although I am pretty sure we could keep both approaches working. The theming was developed during Silverlight 2 where a Style could be applied only once and no implicit Styles were available. I think we outlived its usefulness. Perhaps with the release of Silverlight 5 we will be able to transition to the approach you are pointing here.
In general if you create a project, reference the Controls and Input as well as the Office_Blue and Office_Silver you should be able to use the following xaml:
With the code behind that will swap themes:
using
System;
using
System.Windows;
using
System.Windows.Controls;
namespace
SilverlightThemingThroughImplicitStyles
{
public
partial
class
MainPage : UserControl
{
public
MainPage()
{
InitializeComponent();
}
private
void
Black_Click(
object
sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Controls;component/themes/System.Windows.xaml"
, UriKind.RelativeOrAbsolute) });
}
private
void
Blue_Click(
object
sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Blue;component/themes/System.Windows.xaml"
, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Blue;component/themes/Telerik.Windows.Controls.xaml"
, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Blue;component/themes/Telerik.Windows.Controls.Input.xaml"
, UriKind.RelativeOrAbsolute) });
}
private
void
Silver_Click(
object
sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Silver;component/themes/System.Windows.xaml"
, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Silver;component/themes/Telerik.Windows.Controls.xaml"
, UriKind.RelativeOrAbsolute) });
Application.Current.Resources.MergedDictionaries.Add(
new
ResourceDictionary() { Source =
new
Uri(
"/Telerik.Windows.Themes.Office_Silver;component/themes/Telerik.Windows.Controls.Input.xaml"
, UriKind.RelativeOrAbsolute) });
}
}
}
And it works in Silverlight like a charm swapping themes at runtime. It even changes the theme of the CheckBox. It will not work so flawless in WPF since we have custom DefaultStyleKeys set based on the theme for each control but I think we could nail it down there too. Also if you want to use custom styles for your own UserControls you could pretty much merge your own dictionaries the same way.
Project included.
I have no idea what the problem in your demo is. I'd bet its something small like mismatched colors -placed Red in the Purple theme and vice versa?
Regards,
Pana
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>