title: How to change the color theme at runtime
body: <p>I am working on a WPF application and I am trying to change colors during runtime. I tried the solution posted in this thread: <a href="https://stackoverflow.com/questions/786183/wpf-changing-resources-colors-from-the-app-xaml-during-runtime">WPF: Changing Resources (colors) from the App.xaml during runtime</a>; however, the solution doesn't seem to work.</p>
  
  <p>I have 3 resource dictionaries defined</p>
  
  <p>Colors1.xaml</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;Color x:Key="BaseColor"&gt;#ffaa01&lt;/Color&gt;
    &lt;SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Colors2.xaml</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;Color x:Key="BaseColor"&gt;#aaff01&lt;/Color&gt;
  &lt;SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Colors3.xaml</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;Color x:Key="BaseColor"&gt;#aa01ff&lt;/Color&gt;
  &lt;SolidColorBrush x:Key="BackgroundColorBrush" Color="{DynamicResource BaseColor}" /&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>All 3 resource dictionaries are located in a sub folder within the root of my application called "Resources".</p>
  
  <p>I have added the Colors1 resource dictionary to the Application resources (in the Application.xaml file) so that it is loaded by default.</p>
  
  <p>Here is my Application.xaml:</p>
  
  <pre><code>&lt;Application x:Class="Application"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  StartupUri="Window1.xaml"&gt;
     &lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary x:Name="Colors1" Source="Resources/Colors1.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
     &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>I have a Window in my application called "Window1" which contains a Grid and a ComboBox that allows you to switch between resource dictionaries.</p>
  
  <p>Here is the xaml code for the Window1:</p>
  
  <pre><code>&lt;Window x:Class="Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Picture Orders" Height="600" Width="600"
  xmlns:myProj="clr-namespace:TryingWPF"&gt;
  &lt;Window.Resources&gt;
      &lt;x:Array x:Key="ResourceNames" Type="sys:String"
               xmlns:sys="clr-namespace:System;assembly=mscorlib"&gt;
          &lt;sys:String&gt;Colors1&lt;/sys:String&gt;
          &lt;sys:String&gt;Colors2&lt;/sys:String&gt;
          &lt;sys:String&gt;Colors3&lt;/sys:String&gt;
      &lt;/x:Array&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid x:Name="VisualRoot" Background="{DynamicResource BackgroundColorBrush}"&gt;
      &lt;ComboBox x:Name="ResourceOptions" 
                ItemsSource="{StaticResource ResourceNames}" 
                SelectedIndex="0"
                VerticalAlignment="Top"
                HorizontalAlignment="Center"
                SelectionChanged="ResourceOptions_SelectionChanged"/&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And here is the VB.NET code that handles the Selection Changed event for the ComboBox that should change the background color for the Grid in the Window:</p>
  
  <pre><code>Public Class Window1
  
     Private Sub ResourceOptions_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
      Dim resourceSource As String = String.Format("pack://application:,,,/Resources/{0}.xaml", ResourceOptions.SelectedValue)
      Dim newResourceDictionary As New ResourceDictionary()
      newResourceDictionary.Source = New Uri(resourceSource)
      Application.Current.Resources.MergedDictionaries.RemoveAt(0)
      Application.Current.Resources.MergedDictionaries.Insert(0, newResourceDictionary)
  
     End Sub
  End Class
  </code></pre>
  
  <p>I'm not sure what I'm doing wrong.</p>
  
  <p>How do I get the colors to change?</p>
  
  <p><strong>*Edit: *</strong>
  Well, right after posting this question, I discovered that if I moved the Brush out of the color resource file and into the Window resources, the color would change upon switching resources.</p>
  
  <p>However, If I move the brush to another ResourceDictionary, called ColorBrushes, and simply switch the Color ResourceDictionaries, it would not change colors. </p>
  
  <p>In my main application, all of my brushes are defined in ResourceDictionaries, not within the windows or user controls themselves....so this is still a problem for me.</p>