title: How can I bind a command of a control in template to my ViewModel
body: <p>Right, what I have is:</p>
  
  <pre><code>&lt;Window x:Class="WpfGettingThingsDone.View.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      AllowsTransparency="True"
      Background="Transparent"
      WindowStyle="None"    
      Title="{Binding Title}" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;Style x:Key="WindowBorderBackground" TargetType="{x:Type Border}"&gt;
                  &lt;Setter Property="Background"&gt;
                      &lt;Setter.Value&gt;
                          &lt;LinearGradientBrush StartPoint="0,1" EndPoint="1,0"&gt;
                              &lt;GradientStop Color="#FF222222" Offset="0" /&gt;
                              &lt;GradientStop Color="#FF222222" Offset="0.2" /&gt;
                              &lt;GradientStop Color="#FFAAAAAA" Offset="0.6" /&gt;
                              &lt;GradientStop Color="#FF222222" Offset="0.7" /&gt;
                              &lt;GradientStop Color="#FFAAAAAA" Offset="0.9" /&gt;
                              &lt;GradientStop Color="#FF222222" Offset="1" /&gt;
                          &lt;/LinearGradientBrush&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
  
              &lt;Style x:Key="WindowHeaderedContent" TargetType="{x:Type HeaderedContentControl}"&gt;
                  &lt;Setter Property="HeaderTemplate"&gt;
                      &lt;Setter.Value&gt;
                          &lt;DataTemplate&gt;
                              &lt;Border 
                                  Background="Black"
                                  BorderBrush="Black" 
                                  BorderThickness="1"
                                  CornerRadius="5,5,0,0"
                                  Padding="4"
                                  SnapsToDevicePixels="True"
                                  &gt;
                                  &lt;DockPanel&gt;
                                      &lt;Button DockPanel.Dock="Right" Command="{Binding Path=CloseCommand}"&gt;X&lt;/Button&gt;
                                      &lt;TextBlock
                                          FontSize="14"
                                          FontWeight="Bold"
                                          Foreground="White"
                                          HorizontalAlignment="Center"
                                          Text="{TemplateBinding Content}" /&gt;
                                  &lt;/DockPanel&gt;
                              &lt;/Border&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Style&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Border CornerRadius="5" Style="{StaticResource WindowBorderBackground}"&gt;
          &lt;HeaderedContentControl Header="Current Contexts" 
                                  Style="{StaticResource WindowHeaderedContent}" 
                                  &gt;
          &lt;/HeaderedContentControl&gt;
      &lt;/Border&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Basically draws a window with a pretty gradient background, using a HeaderedContentControl to create the title bar, which uses a HeaderTemplate to put the x button there.</p>
  
  <p>Like so:</p>
  
  <p><img src="https://i.stack.imgur.com/sp6NM.png" alt="alt text"></p>
  
  <p>However, as you can see, I've tried binding the command of the X (close) button to the CloseCommand in my ViewModel. Assuming my ViewModel is correct and that my lack of understanding of the WPF databinding stuff is the problem, what am I doing wrong? Can it not be done the way I'm trying?</p>
  
  <p>(Note: For the purposes of this question I merged all resources in use by the window into the windows resource dictionary.)</p>
  
  <p><strong>Edit:</strong> Since Sam suggested my DataContext for my window isn't set, I'll clarify that it is set, but done in the code behind for App.Xaml when it creates the MainWindow.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Interaction logic for App.xaml
  /// &lt;/summary&gt;
  public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
  
          MainWindow mainWindow = new MainWindow();            
          var viewModel = new MainWindowViewModel();
  
          viewModel.RequestClose += (s, ev) =&gt; mainWindow.Close();
  
          mainWindow.DataContext = viewModel;
  
          mainWindow.Show();
      }
  }
  </code></pre>