title: how to add event handler to control in datatemplate in resource dictionary
body: <p>I have a resource dictionary:</p>
  
  <pre><code>&lt;ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="wpfUI2.MainWindowEvents"&gt;
  
  
  &lt;DataTemplate
      x:Key="WorkspacesTemplate"&gt;
      &lt;TabControl
          x:Name="Tab1"
          IsSynchronizedWithCurrentItem="True"
          ItemsSource="{Binding}"
          ItemTemplate="{StaticResource ClosableTabItemTemplate}"
          Margin="4"/&gt;
  &lt;/DataTemplate&gt;
  ...
  </code></pre>
  
  <p>And I want to add an event handler to the TabControl.  MainWindowEvents is a class defined in a file with no other classes:</p>
  
  <pre><code>Namespace wpfUI2
      Public Class MainWindowEvents
  
      End Class
  End Namespace
  </code></pre>
  
  <p>When I go to add an event handler like</p>
  
  <pre><code>    &lt;TabControl
          x:Name="Tab1"
          IsSynchronizedWithCurrentItem="True"
          ItemsSource="{Binding}"
          ItemTemplate="{StaticResource ClosableTabItemTemplate}"
          Margin="4"
          SelectionChanged=""
      /&gt;
  </code></pre>
  
  <p>and try to click between the "" to create the event I get an error saying that the class specified by the x:Class attribute must be the first in the file.  Well it is!.  Strangely, when I create the handler manually:</p>
  
  <pre><code>Namespace wpfUI2
      Public Class MainWindowEvents
          Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
  
          End Sub
      End Class
  End Namespace
  </code></pre>
  
  <p>Everything compiles ok, but i get a runtime exception on window.show</p>
  
  <p>What am i doing wrong?</p>