title: WPF Dynamic Resource Lookup Behavior with FixedPage
body: <p>Having the following very simple xaml:</p>
  
  <p></p>
  
  <pre><code>&lt;DocumentViewer Name="dv"&gt;
      &lt;FixedDocument Name="fd" Loaded="fd_loaded"&gt;
          &lt;FixedDocument.Resources&gt;
              &lt;Style x:Key="TestStyle"&gt;
                  &lt;Style.Setters&gt;
                      &lt;Setter Property="TextBlock.Foreground" Value="BlueViolet"/&gt;
                  &lt;/Style.Setters&gt;
              &lt;/Style&gt;
              &lt;SolidColorBrush x:Key="foregroundBrush" Color="Orange"/&gt;
          &lt;/FixedDocument.Resources&gt;
          &lt;PageContent Name="pc"&gt;
              &lt;FixedPage Name="fp" Width="800" Height="600" Name="fp"&gt;
                  &lt;TextBlock Name="tb" Style="{DynamicResource TestStyle}"&gt;
                          Lorem ipsum
                  &lt;/TextBlock&gt;
                  &lt;TextBlock Foreground="{DynamicResource foregroundBrush}" Margin="20"&gt;
                          Lorem ipsum
                  &lt;/TextBlock&gt;
              &lt;/FixedPage&gt;
          &lt;/PageContent&gt;
      &lt;/FixedDocument&gt;
  &lt;/DocumentViewer&gt;
  </code></pre>
  
  <p>The use of Dynamic Resources (which I actually need in a more complex situation) here doesn't work. Using Static Resources colors the TextBlocks in the desired colors. Moving the Resources to the level of the FixedPage also does the trick. But I would like to have one generic resource dictionary on a top level element (because of runtime changes the user can make for colours, fonts, etc.). Placing the resources on Application level also does work. But it's not an option for good reasons.</p>
  
  <p>Anybody have any clue why this doesn't work. Does it have something to do with the Logical Tree from the TextBlock upwards? </p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ms750613.aspx" rel="nofollow">MSDN Resources Overview</a> states that:</p>
  
  <p>The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.</p>
  
  <ul>
  <li>If the element defines a Style property, the Resources dictionary within the Style is checked.</li>
  <li>If the element defines a Template property, the Resources dictionary within the FrameworkTemplate is checked.</li>
  </ul>
  
  <p>The lookup process then traverses the logical tree upward, to the parent element and its resource dictionary. This continues until the root element is reached.</p>
  
  <p>I also tried putting the Brush and the Style into the Resources of a (dummy) Style according to the above explanation of MSDN. But that didn't work either.</p>
  
  <p>Really have the feeling that this can not be that complex, but most probably I oversee something. Any help is appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When naming the TextBlock to "tb" and then using tb.FindResource("TestStyle") throws an exception. So that resource clearly can't be found. If I check out LogicalTreeHelper.GetParent(tb) and repeat that for the parents found I get the expected result: TextBlock > FixedPage > PageContent > FixedDocument ...</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>This works perfect. What's the difference with the XAML projected earlier?</p>
  
  <pre><code>&lt;Window x:Class="WpfDynamicStyles2.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;Grid&gt;
          &lt;Grid.Resources&gt;
              &lt;SolidColorBrush x:Key="resBrush" Color="Orange"&gt;&lt;/SolidColorBrush&gt;
          &lt;/Grid.Resources&gt;
              &lt;StackPanel&gt;
              &lt;Button&gt;
                  &lt;TextBlock Foreground="{DynamicResource resBrush}"&gt;Dummy text...&lt;/TextBlock&gt;
              &lt;/Button&gt;           
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>EDIT3</strong></p>
  
  <pre><code>private void fd_Loaded(object sender, RoutedEventArgs e)
  {
      Object obj = pc.TryFindResource("foregroundBrush");
      obj = fp.TryFindResource("foregroundBrush");
      obj = tb.TryFindResource("foregroundBrush");
  }
  </code></pre>
  
  <p>The dynamic resource placed on the Foreground property of the textbox cannot be resolved (the actual resource is at the FixedDocument.Resources level). Also using the TryFindResource in code behind works from pc (PageContent) but from fp (FixedPage) and tb (TextBlock) it cannot resolve the resource (obj is null). When using a Static Resource in the XAML Markup everything works fine.</p>