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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With