Changing Silverlight application themes at runtime
Posted
on Dot net Slackers
See other posts from Dot net Slackers
Published on Wed, 17 Mar 2010 00:00:00 GMT
Indexed on
2010/03/17
10:51 UTC
Read the original article
Hit count: 285
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 Teleriks online demos for Silverlight, the demos for WPF and help documentation for WPF and help documentation for Silverlight.
Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.
© Dot net Slackers or respective owner