title: WPF Dynamically change resource file and theme
body: <p>My project uses a ProjectTheme.xaml file for all WPF windows through out the project.
  The ProjectTheme.xaml file references a style theme as follows</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;ResourceDictionary.MergedDictionaries&gt;
          &lt;!-- In order to modify the project's theme, change this line --&gt;
          &lt;ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized.xaml" /&gt;
      &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>All WPF Windows references WindowBase.xaml</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="/MyProject;component/View/WindowBase.xaml"/&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>WindowBase.xaml references customized titlebar Bar1.xaml</p>
  
  <pre><code>&lt;ResourceDictionary.MergedDictionaries&gt;
      &lt;ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Bar1.xaml" /&gt;
  &lt;/ResourceDictionary.MergedDictionaries&gt;
  </code></pre>
  
  <p>Bar1.xaml references ProjectTheme.xaml</p>
  
  <pre><code>&lt;ResourceDictionary.MergedDictionaries&gt;
      &lt;ResourceDictionary Source="/MyProject;component/ProjectTheme.xaml"/&gt;
  &lt;/ResourceDictionary.MergedDictionaries&gt;
  </code></pre>
  
  <p>So the heriarchy is </p>
  
  <ul>
  <li>Window1 references WindowBase.xaml </li>
  <li>WindowBase references Bar1.xaml</li>
  <li>Bar1 references ProjectTheme.xaml </li>
  <li>ProjectTheme.xaml reference the real theme resource file.</li>
  </ul>
  
  <p>This works fine. 
  Now I want to dynamically change the project theme at run time without quitting the app. 
  Assuming that I have several theme style files</p>
  
  <ul>
  <li>Customized.xaml</li>
  <li>Customized1.xaml</li>
  <li>Customized2.xaml</li>
  </ul>
  
  <p>My question is 
  if it possible to dynamically update ProjectTheme.xaml file at run time to change the line 
  from</p>
  
  <pre><code>&lt;ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized.xaml" /&gt;
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;ResourceDictionary Source="/MyProject;component/Themes/WPFThemes/Customized1.xaml" /&gt;
  </code></pre>
  
  <p>to achieve my objective?
  If yes, how do I do it?
  If no, what is the reason and what is the best (other) way to achieve my purpose?</p>
  
  <p>I have tried the following but none of them work: the style does not change.</p>
  
  <p>way 1</p>
  
  <pre><code>Application.Current.Resources.MergedDictionaries.Clear();
  Uri NewTheme = new Uri(@"/MyProject;component/Themes/WPFThemes/Customized2.xaml", UriKind.Relative);
  ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(NewTheme);
  Application.Current.Resources.MergedDictionaries.Add(dictionary);
  </code></pre>
  
  <p>way 2</p>
  
  <pre><code>Application.Current.Resources.MergedDictionaries.RemoveAt(0);
  Uri NewTheme = new Uri(@"/MyProject;component/Themes/WPFThemes/Customized2.xaml", UriKind.Relative);
  ResourceDictionary dictionary = (ResourceDictionary)Application.LoadComponent(NewTheme);
  Application.Current.Resources.MergedDictionaries.Insert(0, dictionary);
  </code></pre>
  
  <p>Note:
  In my real theme style files (Customized.xaml...) I used a combination of dynamic resource and static resource. Does that matters?</p>
  
  <p>Thanks in advance.</p>