title: Using events and Commands within ItemsControl in WPF
body: <p>Say I have a standard WPF ItemsControl bound to an ObservableCollection of "Dog" objects like so:</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Dogs}"&gt;
  &lt;ItemsControl.ItemTemplate&gt;
      &lt;DataTemplate&gt;
           &lt;StackPanel Orientation="Horizontal"&gt;
               &lt;TextBlock Text="{Binding Name}"/&gt;
               &lt;TextBlock Text="{Binding Breed}"/&gt;
               &lt;TextBlock Text="{Binding Weight}"/&gt;
           &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>I want the user to be able delete any of the dogs in the collection.  In the past I've been doing this with a ListBox control and binding my ViewModel to the SelectedItem property.  I then create a button with an event that removes the selected object from the ObservableCollection.</p>
  
  <p>This works OK but I'd like to lay it out so each row can have its own delete button.</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Dogs}"&gt;
  &lt;ItemsControl.ItemTemplate&gt;
      &lt;DataTemplate&gt;
           &lt;StackPanel Orientation="Horizontal"&gt;
               &lt;TextBlock Text="{Binding Name}"/&gt;
               &lt;TextBlock Text="{Binding Breed}"/&gt;
               &lt;TextBlock Text="{Binding Weight}"/&gt;
               &lt;Button Click="Click_EventHandler"/&gt;
           &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>And an event that looks like this:</p>
  
  <pre><code>private void ListBox_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
      {
            //Delete this Dog Object from Observable Collection
      }
  </code></pre>
  
  <p>Shoving a button into the ItemTemplate and giving it an event crashes WPF, and binding a command to a button within an ItemTemplate doesn't do anything at all so my former method will not work.</p>
  
  <p>The only way I can think of doing this is adding a ToggleButton to the ItemTemplate and binding to the View Model, and then firing an event in the Setter.  Not exactly an elegant solution.</p>
  
  <p>Anyone have any better idea on how to go about this?</p>