title: WPF Style for base window not applied in App.xaml, but is in Themes/Generic.xaml
body: <p>I am in the process of creating a base window class for most of my windows to derive from. Obviously the best solution for this was a separate class, and a style that applies to it.</p>
  
  <p>The issue is that the <code>&lt;Style ../&gt;</code> I have is not being applied when it is in <code>App.Resources</code>. That is, if it's defined in an external <code>ResourceDictionary</code>, and merged into <code>App.xaml</code>'s resources, or a local dictionary and merged, or placed inline into <code>App.Resources</code>. The <code>&lt;Style ../&gt;</code> is, however, applied when it is placed into <code>Themes/Generic.xaml</code>.</p>
  
  <p>The problem can be demonstrated without doing anything special at all in the base window, apart from overriding the <code>DefaultStyleKeyProperty</code>.</p>
  
  <p>Below is <code>ThemeWindow</code>:</p>
  
  <pre><code>public class ThemeWindow : Window
  {
      static ThemeWindow()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(ThemeWindow), new FrameworkPropertyMetadata(typeof(ThemeWindow)));
      }
  }
  </code></pre>
  
  <p>Here is the very simple <code>&lt;Style ../&gt;</code> I am trying to apply (it makes the <code>Window</code> background red, nothing more):</p>
  
  <pre><code>&lt;Style TargetType="{x:Type testing:ThemeWindow}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type testing:ThemeWindow}"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.Background&gt;
                          &lt;SolidColorBrush Color="Red"/&gt;
                      &lt;/Grid.Background&gt;
                      &lt;AdornerDecorator&gt;
                          &lt;ContentPresenter /&gt;
                      &lt;/AdornerDecorator&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>The <code>MainWindow</code> that uses <code>ThemeWindow</code>, is simply the following XAML:</p>
  
  <pre><code>&lt;testing:ThemeWindow x:Class="Testing.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:testing="clr-namespace:Testing"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;Button Content="Button" HorizontalAlignment="Left" Margin="125,83,0,0" VerticalAlignment="Top" Width="75"/&gt;
      &lt;/Grid&gt;
  &lt;/testing:ThemeWindow&gt;
  </code></pre>
  
  <p>Now, as stated, if you place that <code>Style</code> in its own <code>ResourceDictionary</code>, and include it like this:</p>
  
  <pre><code>&lt;App.Resources&gt;
      &lt;ResourceDictionary&gt;
          &lt;ResourceDictionary.MergedDictionaries&gt;
              &lt;ResourceDictionary Source="/Themes/ThemeWindow.xaml" /&gt;
          &lt;/ResourceDictionary.MergedDictionaries&gt;
      &lt;/ResourceDictionary&gt;
  &lt;/App.Resources&gt;
  </code></pre>
  
  <p>.. it does not work. If you inline the style straight into <code>App.Resources</code>, it does not work.</p>
  
  <p>The <em>only</em> situation I can find it working is to call the <code>ResourceDictionary</code> xaml <code>Generic.xaml</code>, and place it into the <code>Themes/</code> directory of the application.</p>
  
  <p>I am wondering exactly why this is happening.</p>
  
  <p>My only theory is that when WPF sees a control type, it will head over to <code>Themes</code>, and scan all <code>ResourceDictionary</code>s for the type, then fall back to <code>Generic.xaml</code> and load it. This doesn't explain why it would not load if the <code>&lt;Style /&gt;</code> is available in a merged <code>ResourceDictionary</code> though. <strong>Note</strong> that it does work if the <code>MergedDictionary</code> is placed into <code>Generic.xaml</code>, for obvious reasons.</p>
  
  <p>I'm perfectly fine with having to merge the <code>ResourceDictionary</code> into <code>Generic.xaml</code> if that's what I have to do. I just want to get down at the technical details as to why it needs to be like this.</p>
  
  <p>Screenshots of this not working / working:
  <img src="https://i.stack.imgur.com/D7tXK.png" alt="Broken Image">
  <img src="https://i.stack.imgur.com/LFavj.png" alt="Working Image"></p>