title: MVVM Execute Command from a User Control
body: <p>I have an MVVM application and part of the functionality is issuing a command in my viewmodel.  The control which is bound to the command happens to reside in the rows of a DataGrid.  Here is some XAML for that:</p>
  
  <pre><code>&lt;DataGridTemplateColumn Width="25"&gt;
      &lt;DataGridTemplateColumn.CellTemplate&gt;
          &lt;DataTemplate&gt;
  
              &lt;!--&lt;base:DeleteButton  HorizontalAlignment="Center" ToolTip="Delete Polygon"
             Visibility="{Binding Path=CanDeleteFromUI, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}" 
  Command="{Binding Path=DataContext.DeletePolygonCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}"/&gt;--&gt;
  
              &lt;CheckBox Template="{StaticResource RemoveXButtonTemplate}" Margin="0,0,3,0" HorizontalAlignment="Center" ToolTip="Delete Polygon" Cursor="Hand" Visibility="{Binding Path=CanDeleteFromUI, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"
      Command="{Binding Path=DataContext.DeletePolygonCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
      CommandParameter="{Binding}" &gt;
              &lt;/CheckBox&gt;
          &lt;/DataTemplate&gt;
      &lt;/DataGridTemplateColumn.CellTemplate&gt;
  &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p>Notes: For various reasons, the "button" is actually a checkbox.</p>
  
  <p>This works just fine as coded. Note there is a commented-out user control, which I can't get to work properly, but the "real" checkbox works just fine.</p>
  
  <p>Here is XAML for the user control:</p>
  
  <pre><code>&lt;UserControl x:Class="Athena.Infrastructure.DeleteButton"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" &gt;
      &lt;Grid&gt;
          &lt;CheckBox Command="{Binding Path=Command}" CommandParameter="{Binding Path=CommandParameter}" Cursor="Hand"&gt;
              &lt;CheckBox.Template&gt;
                  &lt;ControlTemplate&gt;
                      &lt;Border Width="14" Height="14" Background="#00000000" Margin="2,0,2,0"&gt;
                      ....
                      ....
                  &lt;/ControlTemplate&gt;
              &lt;/CheckBox.Template&gt;
          &lt;/CheckBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>I didn't include all the XAML for the control template, as everything displays just fine.</p>
  
  <p>Here is the code-behind:</p>
  
  <pre><code>public partial class DeleteButton : UserControl
  {
      public DeleteButton()
      {
          InitializeComponent();
      }
  
      public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(DeleteButton));
      public ICommand Command
      {
          get { return (ICommand) GetValue(CommandProperty); }
          set{SetValue(CommandProperty, value);}
      }
  
      public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(DeleteButton));
      public object CommandParameter
      {
          get { return (object) GetValue(CommandParameterProperty); }
          set { SetValue(CommandParameterProperty, value); }
      }
  }
  </code></pre>
  
  <p>﻿﻿I didn't show my VM code, but it contains the usual 3 methods, Command, CanExecute and Method.  When I use the checkbox, the Command executes once (to initialize the Method), and thereafter the Method does fire when I click the checkbox.</p>
  
  <p>When I use the user control instead of the normal checkbox, the Command in my VM fires once as expected. Thereafter, the method does not fire at all.</p>
  
  <p>It seems that this is a common thing, to use a User Control instead of a native control, right?  I can't figure out why my VM method will not fire using the User Control.</p>
  
  <p>Can anybody shed some light on this for me?  Thanks so much...</p>