switching wpf resource dictionaries at runtime

a 夏天 提交于 2019-12-20 11:52:57

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/2381085/switching-wpf-resource-dictionaries-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!