Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching wpf resource dictionaries at runtime

I am trying to build a wpf application that allows the user to change the theme at runtime. What I have done so far is create a resourcedictionary with all the colors for the application defined in it and then I am binding to this dictionary in the xaml.

Below is the code I have for switching the resource dictionary:

if (System.IO.File.Exists(fileName))
{
   using (FileStream fs = new FileStream(fileName, FileMode.Open))
   {
      ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
      Resources.MergedDictionaries.Clear();
      Resources.MergedDictionaries.Add(dic);
   }
}

This code runs fine, and I know that it is switching the resource dictionary, but it does not update elements already displayed on the screen. My question is: how can I refresh or rebind the screen to take into account the new resource dictionary?

thanks

sm

like image 763
smauel Avatar asked Mar 04 '10 16:03

smauel


1 Answers

In my case, I simply had to change:

Resources.MergedDictionaries.Clear();
Resources.MergedDictionaries.Add(dic);

to:

Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(dic);
like image 171
smauel Avatar answered Nov 19 '22 00:11

smauel