title: Resource Dictionary DataTemplate does not recognise the x:Name
body: <p>I have this <code>ResourceDictionary</code> named as <code>Resource1.xaml</code>:</p>
  <pre><code>&lt;ResourceDictionary xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
                      xmlns:vm=&quot;clr-namespace:WpfApp2&quot;&gt;
      &lt;DataTemplate DataType=&quot;{x:Type vm:MainViewModel}&quot;&gt;
          &lt;Button x:Name=&quot;SomeButton&quot;  /&gt;
      &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  <p><strong>Goal</strong></p>
  <p>I am aiming to add some code code behind without using the ViewModel.</p>
  <p><strong>What I did</strong></p>
  <p>I added a class called <code>Resource1.xaml.cs</code>:</p>
  <pre><code>namespace WpfApp2
  {
      public partial class Resource1
      {
  
      }
  }
  </code></pre>
  <p>And the structure now looks like so:</p>
  <p><a href="https://i.stack.imgur.com/Djgl9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Djgl9.png" alt="enter image description here" /></a></p>
  <p>I have linked the class to the xaml. And also added a click event for the button in the <code>Resource1.xaml</code>:</p>
  <pre><code>&lt;ResourceDictionary xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
                      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
                      xmlns:vm=&quot;clr-namespace:WpfApp2&quot;
                      x:Class=&quot;WpfApp2.Resource1&quot;&gt;
      &lt;DataTemplate DataType=&quot;{x:Type vm:MainViewModel}&quot;&gt;
          &lt;Button x:Name=&quot;SomeButton&quot; Click=&quot;SomeButton_Click&quot; /&gt;
      &lt;/DataTemplate&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  <p>Which then added me an event handler in the <code>Resource1</code> class:</p>
  <pre><code>public partial class Resource1
  {
      private void SomeButton_Click(object sender, System.Windows.RoutedEventArgs e)
      {
          
      }
  }
  </code></pre>
  <p>But when I try getting the <code>x:Name=&quot;SomeButton&quot;</code> in the class, it does not recognise it..</p>
  <p><a href="https://i.stack.imgur.com/EXWgv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EXWgv.png" alt="enter image description here" /></a></p>
  <p><strong>Question</strong></p>
  <p>Is it possible to achieve what I am trying to do using Resource Dictionary? If so, where do I correct it to make it work?</p>