title: Why the StaticResource cannot be resolved in this case?
body: <p>I have got an exception <strong>"Cannot find resource named 'mrg'. Resource names are case sensitive."</strong> when I try to do the following:</p>
  
  <p>MainWindow.xaml:</p>
  
  <pre><code>&lt;Window.Resources&gt;
    &lt;Thickness Left="0"
               Right="1"
               Bottom="2"
               Top="3"
               x:Key="mrg" /&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
    &lt;ItemsControl ItemsSource="{Binding}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
        &lt;DataTemplate&gt;
          &lt;local:UserControl1 /&gt;
        &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
    &lt;/ItemsControl&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
          List&lt;string&gt; source = new List&lt;string&gt;()
          {
              "item1",
              "item2",
              "item3",
              "item4",
              "item5",
          };
          DataContext = source;
      }
  }
  </code></pre>
  
  <p>and UserControl1.xaml:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;TextBlock Text="{Binding}" Margin="{StaticResource mrg}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>According to the <a href="http://msdn.microsoft.com/en-us/library/ms750613(v=vs.100).aspx" rel="noreferrer">msdn article</a>:</p>
  
  <p>Static resource lookup behavior</p>
  
  <ol>
  <li><p>The lookup process checks for the requested key within the resource dictionary defined by the element that sets the property.</p></li>
  <li><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></li>
  <li><p>Next, application resources are checked. Application resources are those resources within the resource dictionary that is defined by the Application object for your WPF application.</p></li>
  </ol>
  
  <p>So the resource had to be found because of step 2. But, as I can see in the <code>Locals</code> window when exception is catched, the <code>UserControl1.Parent == null</code>.</p>
  
  <p>I'm confused in this problem. The way I can solve it is to put the resource to the Application level.</p>
  
  <p>My question is: why the StaticResource connot be found ?</p>