We have received a lot of questions how can the application theme be changed at run time. The most important thing here to mark is that each time the application theme is changed all the controls should be re-drawn. Without going into too much detail, we could explain the application themes as a mechanism to replace the content of the Generic.xaml file in every loaded Telerik assembly at runtime. This does not affect the controls that already have default style applied, hence the need to create new instances.
Because in the Silverlight applications the RootVisual cannot be changed at run time, we need a way to reset the application UI. The following code is in App.xaml.cs.

private void Application_Startup(object sender, StartupEventArgs e)
    {
          // Before:
          // this.RootVisual = new MainPage();
  
        this.RootVisual = new Grid();
        this.ResetRootVisual();
    }
  
    public void ResetRootVisual()
    {
        var rootVisual = Application.Current.RootVisual as Grid;
        rootVisual.Children.Clear();
        rootVisual.Children.Add(new MainPage());
    }

 

In Application_Startup() instead of creating new MainPage UserControl instance as RootVisual, we create a new Grid panel, that will contain the MainPage UserControl. In the ResetRootVisual() method we create the instance of MainPage and add it to the RootVisual panel.
Then we have to create a method in the code behind which will set StyleManager.ApplicationTheme and then will call the ResetRootVisual() method:

private void ChangeApplicationTheme(Theme theme)
{
    StyleManager.ApplicationTheme = theme;
    (Application.Current as App).ResetRootVisual();
}

 

Here you can find an example which illustrates the described implementation of a Silverlight theme. For more information please refer to Telerik’s online demos for Silverlight, the demos for WPF and help documentation for WPF and help documentation for Silverlight.


About the Author

Panayot Cankov

 is Senior Software Developer in Telerik XAML Team

Related Posts

Comments

Comments are disabled in preview mode.