title: Organizing Xaml - Unable to apply StaticResource from different xaml file
body: <p>I have a situation where the wpf application is not able to pickup StaticResource and instead fails with <strong>XamlParseException</strong>. But if I used a DynamicResource instead, the resource is found and no exception occurs.</p>
  
  <p>I was trying to style and organize wpf resources as recommended at <a href="http://projekt202.com/blog/2010/xaml-organization/" rel="nofollow">http://projekt202.com/blog/2010/xaml-organization/</a></p>
  
  <p>I have 2 projects accordingly, a wpf control library that houses all resources and a main wpf project which uses those resources. Here is the structure of the 2 projects</p>
  
  <pre><code>Projects Structure
  </code></pre>
  
  <p><strong>Wpf_Theme.ControlLibrary</strong><br/>
  --ResourceDictionaries<br/>
  ----BaseControlStyles<br/>
  ------ButtonStyle.xaml<br/>
  ------TextBoxStyle.xaml<br/>
  ----Brushes<br/>
  ------DefaultBlueTheme<br/>
  ----ResourceLibrary.xaml<br/>
  <strong>Wpf_Theme.Main</strong><br/>
  --App.xaml<br/>
  --MainWindow.xaml</p>
  
  <p><br/></p>
  
  <pre><code>Contents of xaml files
  </code></pre>
  
  <p><strong>ButtonStyle.xaml</strong></p>
  
  <p></p>
  
  <pre><code>&lt;Style TargetType="{x:Type Button}"&gt;
      &lt;Setter Property="Background"  Value="{StaticResource ControlBackground}"/&gt;
      &lt;Setter Property="BorderBrush" Value="{StaticResource BorderColor}"/&gt;
      ...
  &lt;/Style&gt;
  </code></pre>
  
  <p><strong>DefaultBlueTheme.xaml</strong> (Brushes)</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;SolidColorBrush x:Key="PanelBackground" Color="#C8DCF0"/&gt;
      &lt;SolidColorBrush x:Key="BorderColor" Color="#6A8FB5"/&gt;
      &lt;SolidColorBrush x:Key="SelectedItemBackground" Color="Wheat"/&gt;
      &lt;SolidColorBrush x:Key="TextForeground" Color="Black"/&gt;
  
      &lt;LinearGradientBrush x:Key="ControlBackground" StartPoint="0,0" EndPoint="0,1"&gt;
          &lt;LinearGradientBrush.GradientStops&gt;
              &lt;GradientStop Offset="0" Color="#DBECFD"/&gt;
              &lt;GradientStop Offset="0.5" Color="#C7DBEF"/&gt;
              &lt;GradientStop Offset="1" Color="#B0CAE5"/&gt;
          &lt;/LinearGradientBrush.GradientStops&gt;
      &lt;/LinearGradientBrush&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p><strong>ResourceLibrary.xaml</strong> (Merges all dictionaries in one file to be used by main project)</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;ResourceDictionary Source="Brushes/DefaultBlueTheme.xaml"/&gt;
          &lt;ResourceDictionary Source="BaseControlStyles/ButtonStyle.xaml"/&gt;
      &lt;/ResourceDictionary.MergedDictionaries&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p><strong>App.xaml</strong> (In Main project)</p>
  
  <pre><code>&lt;Application x:Class="Wpf_Themes.Main.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               StartupUri="MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary 
                      Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/ResourceLibrary.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window x:Class="Wpf_Themes.Main.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;StackPanel Width="200" Margin="10"&gt;
          &lt;Button&gt;Click Me&lt;/Button&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>As stated earlier in the post, I am not able to resolve the style(Background and Border brushes) for the <strong>Button</strong> using the <strong>StaticResource</strong> applied in <strong>ButtonStyle.xaml</strong>. If I use <strong>DynamicResource</strong> instead, the brushes are found correctly and applied to the Button. Any insights why this behavior occurs. </p>
  
  <p><br/>
  <strong>Edit:</strong> <br/>
  Following Mike's suggestion, I included the xaml files from the <strong>Wpf_Theme.ControlLibrary</strong> project directly into the <strong>App.xaml</strong> of the Main project like below</p>
  
  <p><strong>App.xaml</strong></p>
  
  <pre><code>&lt;Application x:Class="Wpf_Themes.Main.App"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               StartupUri="MainWindow.xaml"&gt;
      &lt;Application.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/Brushes/DefaultBlueTheme.xaml"/&gt;
                  &lt;ResourceDictionary Source="/Wpf_Themes.ControlLibrary;component/ResourceDictionaries/BaseControlStyles/ButtonStyle.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  
  <p>and resources are now located correctly using the StaticResource</p>