title: DynamicResources fail to load in a programmatically created control
body: <p>I have a WPF (3.5) application using Prism to programmatically instantiate a number of views and then add them to a region.  The problem I am seeing is that styles inside the view that are applied as DynamicResources are not being applied the first time the view is shown.  If we change screens and come back, it will be loaded properly, fairly certain this is due to loading and unloading the control.<br>
  The styles that fail are the ones are defined in our root view.  The root view is in the same class library as the child view, adding them to Application Resources is not an option, however it does appear to fix the problem.</p>
  
  <p>I've replicated the problem in a sample application.</p>
  
  <pre><code> &lt;Window x:Class="ProgrammaticDynamicResourceProblem.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
          xmlns:l="clr-namespace:ProgrammaticDynamicResourceProblem" 
          Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;Style x:Key="RedTextStyle" TargetType="TextBlock"&gt;
              &lt;Setter Property="Foreground" Value="Red" /&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
      &lt;StackPanel x:Name="root"&gt;
          &lt;l:TestUC /&gt; &lt;!-- Will have a foreground of Red --&gt;
      &lt;/StackPanel&gt;
   &lt;/Window&gt;
  </code></pre>
  
  <p>The sample UserControl</p>
  
  <pre><code>&lt;UserControl x:Class="ProgrammaticDynamicResourceProblem.TestUC"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;TextBlock Text="Test Text" Style="{DynamicResource RedTextStyle}" /&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>In the MainWindow constructor I add another instance of a TestUC.</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      root.Children.Add(new TestUC());
  }
  </code></pre>
  
  <p>When the application loads, the first instance will have a foreground of Red as expected, the one added from the constructor will be the default black.</p>
  
  <p>Interestingly, if I modify the constructor to look like this, it works.</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
      root.Children.Add(new TestUC());
      var x = root.Children[1];
      root.Children.RemoveAt(1);
      root.Children.Add(x);
  }
  </code></pre>
  
  <p>Is there a decent solution to getting this to work?  Adding the resources to the Application Resources doesn't work because we have other shells in the same application and these resources are shell specific.  We can merge the resource dictionaries into each of the views and switch them to StaticResources, but there are quite a few views, so we would like to avoid that solution as well.</p>
  
  <p><strong>UPDATE:</strong> Found this <a href="http://connect.microsoft.com/WPF/feedback/details/455990/dynamicresource-style-doesnt-work-when-add-an-element-to-visual-tree-in-onapplytemplate#tabs" rel="nofollow">Connect Issue</a>, but it really didn't help much. </p>