title: Accessing named element inside of window resources
body: <p><strong>Problem:</strong></p>
  
  <p>I'm trying to access a named <code>Run</code> element within a <code>FlowDocument</code> which is defined in the Window Resources. To clarify what I mean, consider the follow code:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;FlowDocument x:Key="doc"&gt;
          &lt;Paragraph&gt;
              &lt;Run x:Name="run" /&gt;
          &lt;/Paragraph&gt;
      &lt;/FlowDocument&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Here, I'd be trying to access the <code>Run</code> element named "run."</p>
  
  <p><strong>What I've Tried So Far:</strong></p>
  
  <ul>
  <li><p>Simply using the element's name to access it. However, named elements in the window's resources apparently don't have the same default accessibility as elements defined in the window's content, because this method did not work.</p></li>
  <li><p>Attempting to add a key to the <code>Run</code> element and then accessing the element through the <code>FindResource()</code> method. Unfortunately, it seems that keys cannot be added to nested elements.</p></li>
  <li><p>The following code, which throws a <code>NullReferenceException</code>:</p>
  
  <pre><code>FlowDocument doc = (FlowDocument)FindResource("doc");
  ((Run)doc.FindName("run")).Text = "example text";
  </code></pre></li>
  </ul>