title: WPF behaviour to check/uncheck a checkbox in a list item
body: <p>I have an application with several item controls (treeviews and others) which contain an item template with a checkbox inside. This checkbox checked state is bound to an IsChecked property of the item view model. This works correctly when clicking on the checkbox, but it's impossible to check/uncheck them with the keyboard (I believe this is due to the fact that the checkbox itself never gets the focus). </p>
  
  <p>I like the solution proposed by DLeh in here: <a href="https://stackoverflow.com/a/24327765/352826">https://stackoverflow.com/a/24327765/352826</a> but I would like an improvement: Instead of having the behaviour calling a command on the base view model (the vm which contains the list of items), I would like the behaviour to directly act on the IsChecked property of the item. 
  My problem is that I don't know how to modify the behaviour or how to set up the binding on it, so that the behaviour can have access to the item's IsChecked property.
  So, instead of the following:</p>
  
  <pre><code>&lt;DataGrid&gt;
      &lt;i:Interaction.Behaviors&gt;
          &lt;shared:ToggleSelectOnSpace ToggleSelectCommand="{Binding Data.ToggleSelectParticipantCommand, Source={StaticResource BindingProxy}}" /&gt;
      &lt;/i:Interaction.Behaviors&gt;
  ...
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I would have something like this:</p>
  
  <pre><code>&lt;DataGrid&gt;
      &lt;i:Interaction.Behaviors&gt;
          &lt;shared:ToggleSelectOnSpace ItemsIsSelectedProperty="{Binding IsChecked}" /&gt;
      &lt;/i:Interaction.Behaviors&gt;
  ...
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  I should add that my current implementation uses the PreviewKeyUp event in the itemscontrol and the following code behind implementation. The problem with this approach is that I have this code in many code behind files, so there is a lot of duplication. My goal is to replace this by a behaviour.</p>
  
  <pre><code>private void TreeView_OnPreviewKeyUp(object sender, KeyEventArgs e)
  {
     if (e.Key == Key.Space)
     {
          var tree = (TreeView) sender;
          var item = tree.SelectedItem as IsSelectedViewModelBase;
          if (item != null)
          {
              item.IsSelected = !item.IsSelected;
              e.Handled = true;
          }
     }
  }
  </code></pre>
  
  <p><strong>Update 2</strong>
  This is the item template and the checkbox is not checked when you press the space bar with the item selected.</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type viewModels:ItemViewModel}" &gt;
      &lt;StackPanel Orientation="Horizontal" &gt;
        &lt;CheckBox Focusable="False" IsChecked="{Binding IsSelected}" VerticalAlignment="Center" /&gt;
        &lt;StackPanel Margin="2"&gt;
             &lt;TextBlock Text="{Binding Username}" FontWeight="Bold" /&gt;
              &lt;TextBlock Text="{Binding FullName}" /&gt;
        &lt;/StackPanel&gt;
     &lt;/StackPanel&gt;
   &lt;/DataTemplate&gt;
  </code></pre>