title: WPF Events in ResourceDictionary for a Window template
body: <p>I'm currently trying to implement a Metro styled Window.<br>
  So i've made the following styles inside a ResoruceDictionary:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  
  &lt;!-- Brushes --&gt;
  &lt;SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFFFF" /&gt;
  
  &lt;!-- Buttons --&gt;
  &lt;Style x:Key="MetroControlBoxButton" TargetType="Button"&gt;
      &lt;Setter Property="Background" Value="{StaticResource BackgroundColor}" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="Button"&gt;
                  &lt;ContentPresenter /&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;!-- Windows --&gt;
  &lt;Style x:Key="MetroWindow" TargetType="Window"&gt;
      &lt;Setter Property="UseLayoutRounding" Value="True" /&gt;
      &lt;Setter Property="WindowStyle" Value="None" /&gt;
      &lt;Setter Property="ResizeMode" Value="NoResize" /&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="Window"&gt;
                  &lt;Grid Background="{StaticResource BackgroundColor}"&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="6" /&gt;
                          &lt;RowDefinition Height="24" /&gt;
                          &lt;RowDefinition Height="*" /&gt;
                          &lt;RowDefinition Height="24" /&gt;
                          &lt;RowDefinition Height="6" /&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="6" /&gt;
                          &lt;ColumnDefinition Width="*" /&gt;
                          &lt;ColumnDefinition Width="6" /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
  
                      &lt;Rectangle Name="topLeftBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="0" /&gt;
                      &lt;Rectangle Name="topCenterBorderRectangle" Fill="Orange" Grid.Row="0" Grid.Column="1" /&gt;
                      &lt;Rectangle Name="topRightBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="2" /&gt;
                      &lt;Rectangle Name="middleLeftBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" /&gt;
                      &lt;Rectangle Name="middleRightBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="2" /&gt;
                      &lt;Rectangle Name="bottomLeftBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="0" /&gt;
                      &lt;Rectangle Name="bottomCenterBorderRectangle" Fill="Orange" Grid.Row="4" Grid.Column="1" /&gt;
                      &lt;Rectangle Name="bottomRightBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="2" /&gt;
  
                      &lt;Rectangle Name="statusBarRectangle" Fill="Yellow" Grid.Row="3" Grid.Column="1" /&gt;
  
                      &lt;Grid Grid.Row="1" Grid.Column="1"&gt;
                          &lt;Grid.RowDefinitions&gt;
                              &lt;RowDefinition Height="*" /&gt;
                          &lt;/Grid.RowDefinitions&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition Width="Auto" /&gt;
                              &lt;ColumnDefinition Width="*"/&gt;
                              &lt;ColumnDefinition Width="28" /&gt;
                              &lt;ColumnDefinition Width="28" /&gt;
                              &lt;ColumnDefinition Width="28" /&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
  
                          &lt;Rectangle Name="dragRectangle" Fill="Yellow" Grid.Row="0" Grid.Column="1" /&gt;
                          &lt;Button Name="minimizeButton" Content="_" Grid.Row="0" Grid.Column="2" Style="{StaticResource MetroControlBoxButton}" /&gt;
                          &lt;Button Name="maximizeButton" Content="[]"  Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" /&gt;
                          &lt;Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" /&gt;
                      &lt;/Grid&gt;
  
                      &lt;ContentPresenter Grid.Row="2" Grid.Column="1" /&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>My problem with is that i'd like to implement the ControlBox buttons (Minimize, Maximize and Close).<br>
  I can create three buttons, align them top right.
  But since my style is inside a ResourceDictionary i have no idea how to implement the click events.</p>
  
  <p>Same goes for resize and dragging the Window. I know how to drag and resize my Window but i need to link it to the Window_MouseLeftButtonDown event, but i have no idea how to link it inside a template.</p>
  
  <p>I saw that i "could" create a class and put my code in it here:<br>
  <a href="https://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handling">Is it possible to set code behind a resource dictionary in WPF for event handling?</a><br>
  But it seems it's not a good approach.</p>
  
  <p>I thought i might be able to create a MetroWindow and have my other Windows inherit it.
  But WPF doesn't support Visual Inheritance.</p>
  
  <p>So what would be the best practice so can reuse my Window template on more than one Window throughout my application.</p>
  
  <p>Thanks,<br>
  SiriusNik</p>