title: Bind button in DataTemplate to command in the form's ViewModel
body: <p>My problem is similar to the one described in this question:<br>
  <a href="https://stackoverflow.com/questions/3772797/wpf-mvvm-button-control-binding-in-datatemplate">WPF MVVM Button Control Binding in DataTemplate</a></p>
  
  <p>Here is my XAML:</p>
  
  <pre><code>&lt;Window x:Class="MissileSharp.Launcher.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MissileSharp Launcher" Height="350" Width="525"&gt;
      &lt;Grid&gt;
          &lt;!-- when I put the button here (outside the list), the binding works --&gt;
          &lt;!--&lt;Button Content="test" Command="{Binding Path=FireCommand}" /&gt;--&gt;
          &lt;ListBox ItemsSource="{Binding CommandSets}"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;!-- I need the button here (inside the list), and here the binding does NOT work --&gt;
                      &lt;Button Content="{Binding}" Command="{Binding Path=FireCommand}" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>It's just a <code>ListBox</code>, bound to an <code>ObservableCollection&lt;string&gt;</code> named <code>CommandSets</code> (which is in the ViewModel).<br>
  This binding works (it displays a button for each item in the collection).</p>
  
  <p>Now I want to bind the button to a command (<code>FireCommand</code>), which is also in the ViewModel.<br>
  Here's the relevant part of the ViewModel:</p>
  
  <pre><code>public class MainWindowViewModel : INotifyPropertyChanged
  {
      public ICommand FireCommand { get; set; }
      public ObservableCollection&lt;string&gt; CommandSets { get; set; }
  
      public MainWindowViewModel()
      {
          this.FireCommand = new RelayCommand(new Action&lt;object&gt;(this.FireMissile));
      }
  
      private void FireMissile(Object obj)
      {
          System.Windows.MessageBox.Show("fire");
      }
  }
  </code></pre>
  
  <p>The binding of this button does NOT work.<br>
  From what I've understood from the <a href="https://stackoverflow.com/questions/3772797/wpf-mvvm-button-control-binding-in-datatemplate">question</a> I linked above, the binding doesn't work because:<br>
  <em>(correct me if I'm wrong)</em></p>
  
  <ul>
  <li>The button is inside the <code>ListBox</code>, so it only "knows" the binding of the <code>ListBox</code> (the <code>ObservableCollection</code>, in this case), but not the binding of the main window</li>
  <li>I'm trying to bind to a command in the main ViewModel of the main window (which the button doesn't "know")</li>
  </ul>
  
  <p>The command itself is definitely correct, because when I put the button <strong>outside</strong> the <code>ListBox</code> (see the XAML above for an example), the binding works and the command is executed.  </p>
  
  <p><strong>Apparently, I "just" need to tell the button to bind to the main ViewModel of the form.<br>
  But I'm not able to figure out the right XAML syntax.</strong></p>
  
  <p>I tried several approaches that I found after some googling, but none of them worked for me:</p>
  
  <pre><code>&lt;Button Content="{Binding}" Command="{Binding RelativeSource={RelativeSource Window}, Path=DataContext.FireCommand}" /&gt;
  
  &lt;Button Content="{Binding}" Command="{Binding Path=FireCommand, Source={StaticResource MainWindow}}" /&gt;
  
  &lt;Button Content="{Binding}" Command="{Binding Path=FireCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" /&gt;
  </code></pre>
  
  <p><strong>Could someone please:</strong></p>
  
  <ol>
  <li>give me the proper XAML to bind the button inside the <code>ListBox</code> to a command in the form's <code>MainViewModel</code>?</li>
  <li>point me to a link where this advanced binding stuff is explained in a way that a WPF/MVVM beginner can understand?<br>
  <em>I'm feeling like I'm just copying and pasting arcane XAML incantations, and so far I don't have any clue (and can't find any good documentation) how I would figure out by myself in which cases I'd need <code>RelativeSource</code> or <code>StaticResource</code> or whatever instead of a "normal" binding.</em></li>
  </ol>