ResourceDictionary in Silverlight consists of styles, pallates, and other static resources.

These static resources – styles – can be bound to UIElements in the view when the view is loaded.  However once the view is loaded, any change in the resources will not affect the loaded view.  Views that are loaded after the change in resource dictionary will undergo the changes.  Let’s take an example:

There are two ResourceDictionary files. One defines the background of Button as ‘Black’ and the other defines it as ‘White’

Initially, Black RD is applied so when the view is created, button (btnButton1) appears black.

Now lets apply White RD dynamically.  This will not change the color of already loaded button (btnButton1) and it will appear as Black.

But any Button that is loaded after this change (application of White RD) will appear White in color.

Now the question comes, how to achieve this change behavior dynamically – through C# code – lets see that.

[sourcecode language=’csharp’]

ResourceDictionary dictionary = Application.Current.Resources as ResourceDictionary; XDocument xaml = XDocument.Load(@”WhiteTheme.xaml”);
ResourceDictionary rd = XamlReader.Load(xaml.ToString()) as ResourceDictionary;
dictionary.MergedDictionaries.Clear();
dictionary.Clear(); dictionary.MergedDictionaries.Add(rd);

[/sourcecode]

This code snippet will load the WhiteTheme.xaml (a ResourceDictionary file) dynamically by removing the existing applied ResourceDictionary (BlackTheme.xaml)