title: Fire a command from a Button inside a ContentControl?
body: <p>I'm new to WPF and I'm trying to dynamically add a Button inside a ContentControl, which should fire a command when clicked. I'm using MVVMLight to handle the Commands.</p>
  
  <p>Below I have an example with two buttons. The top button is placed directly into the StackPanel. This button fires off the Command as expected.</p>
  
  <p>The second button is placed inside a ContentControl. It displays correctly, but the Command does not fire when the button is clicked.
  I assumed this is because the Binding does not transfer down through the DataTemplate, but it seems to work if I use regular Commands instead of MVVMLight RelayCommands.</p>
  
  <p>I don't want to remove the framework, so I'm wondering if anyone knows how to fix it? Thanks</p>
  
  <pre><code>&lt;Window x:Class="ContentControlExample.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vm="clr-namespace:ContentControlExample.ViewModel"&gt;
  
      &lt;Window.DataContext&gt;
          &lt;vm:MainViewModel /&gt;
      &lt;/Window.DataContext&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="MyButton" &gt;
              &lt;Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  
      &lt;StackPanel&gt;
  
          &lt;!--When this button is clicked, the Command executes as expected--&gt;
          &lt;Button Content="SUBMIT" Command="{Binding MyCommand}" Width="200" Height="50"/&gt;
  
  
          &lt;!--Nothing happens when this button is clicked--&gt;
          &lt;ContentControl ContentTemplate="{StaticResource MyButton}"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Here's the ViewModel with the command:</p>
  
  <pre><code>public class MainViewModel : ViewModelBase
  {
          public ICommand MyCommand { get; private set; }
  
          public MainViewModel()
          {
              MyCommand = new RelayCommand(MyCommand_Executed, MyCommand_CanExecute);
          }
  
          private bool MyCommand_CanExecute()
          {
              return true;
          }
  
          private void MyCommand_Executed()
          {
              MessageBox.Show("The command executed");
          }
      }
  </code></pre>