title: How to create a dynamically changeable style with WPF in external library?
body: <p>I am trying to make a library (with WPF) that will contain some of my basic templates for windows , buttons and some another new costume controls and templates for later use in projects . </p>
  
  <p>I am still new to WPF . I understood that i should create a new ResourceDictionary in my external library and put there all my styles and their dynamic variables . And later make a MergedResourceDictionary in my main application project that will give me access to use my usual styles and controls . </p>
  
  <p>Well i success to create a basic experimental style with a template which uses two variables as DynamicResource , Just for testing i made a <code>&lt;Brush x:Key="brush_back_standard"&gt;RoyalBlue&lt;/Brush&gt;</code> . This is used for regular back coloring . Then i declared to use this brush dynamically in the style itself : <code>&lt;Grid Background="{DynamicResource brush_back_standard}"&gt;</code>, And It works well . </p>
  
  <p>My problem begins when i am trying to change the brush value a runtime : <code>Style.Resources[ "brush_back_standard" ] = Brushes.Aqua;</code>. To simulate the case that the user will change the theme in the settings of the application later at runtime . So that didn't work at all , and i got the exception : <code>ResourceDictionary is read-only and cannot be modified.</code> . So if it's really readonly then it's sort of useless to me because the user cannot change the template at runtime . I need to find some way to let the user change <strong>anything</strong> in the window template at run time . </p>
  
  <p>Full style code (external library) :</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:system="clr-namespace:System;assembly=mscorlib"&gt;
  &lt;Style x:Key="window_standard"
         TargetType="{x:Type Window}"&gt;
      &lt;Style.Resources&gt;
          &lt;system:Double x:Key="thickness_shadow"&gt;10&lt;/system:Double&gt;
          &lt;Brush x:Key="brush_back_standard"&gt;RoyalBlue&lt;/Brush&gt;
      &lt;/Style.Resources&gt;
      &lt;Setter Property="WindowStyle"
              Value="None" /&gt;
      &lt;Setter Property="AllowsTransparency"
              Value="True" /&gt;
      &lt;Setter Property="BorderThickness"
              Value="{DynamicResource thickness_shadow}" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Window}"&gt;
                  &lt;Grid Background="{DynamicResource brush_back_standard}"&gt;
                      &lt;AdornerDecorator&gt;
                          &lt;ContentPresenter /&gt;
                      &lt;/AdornerDecorator&gt;
                      &lt;ResizeGrip x:Name="WindowResizeGrip"
                                  HorizontalAlignment="Right"
                                  VerticalAlignment="Bottom"
                                  Visibility="Collapsed"
                                  IsTabStop="false" /&gt;
                  &lt;/Grid&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="ResizeMode"
                               Value="CanResizeWithGrip"&gt;
                          &lt;Setter TargetName="WindowResizeGrip"
                                  Property="Visibility"
                                  Value="Visible" /&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p></p>
  
  <p>Accessing the library in my main application project :</p>
  
  <pre><code>&lt;Application x:Class="diamond.sandbox.executer"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           StartupUri="window_main.xaml"&gt;
  &lt;Application.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="pack://application:,,,/diamond.core;component/xaml/standard.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/Application.Resources&gt;
  </code></pre>
  
  <p></p>
  
  <p>The window_main.xaml which is magically affected by the style :</p>
  
  <pre><code>&lt;Window x:Class="diamond.sandbox.window_main"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="window_main" Height="250" Width="500"
      Style="{DynamicResource window_standard}"
      WindowStartupLocation="CenterScreen"
      Loaded="initialise"&gt;
  </code></pre>
  
  <p></p>
  
  <p>Initialise method (After window_main is loaded) :</p>
  
  <pre><code>private void initialise( object p_sender , RoutedEventArgs p_args )
      {
          Style.Resources[ "brush_back_standard" ] = Brushes.Aqua;
      }
  </code></pre>