title: Which button is clicked DataTemplate WPF MVVM
body: <p>my question here is how to know which button is clicked. My buttons are bound to property of type ObservableCollection which contains objects of type Item and I need to use that object in my ViewModel when a button is clicked. Any ideas how to know which button is clicked? I had few ideas, like sending multiple Command Parameters (1.SelectedItems from ListBox 2.The Object from the button) or bind the object from the button to another property of type Item in the ViewModel after the button is clicked  in order to use it. Any ideas will be apreciated.</p>
  
  <p>I have this DataTemplate for buttons</p>
  
  <pre><code>&lt;DataTemplate x:Key="ButtonTemplate"&gt;
              &lt;WrapPanel&gt;
                  &lt;Button x:Name="OrderButton"
                          FontSize="10"
                          Height="80" Width="80"
                          Content="{Binding Name}"
                          Command="{Binding OrderCommand, 
                                            Source={StaticResource OrderViewModel}}"
                          CommandParameter="{Binding ElementName=ListBoxUserControl, Path=SelectedItems}"&gt;
                  &lt;/Button&gt;
              &lt;/WrapPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>My ViewModel</p>
  
  <pre><code>public class OrderViewModel : ObservableCollection&lt;Order&gt;, INotifyPropertyChanged
  { 
     public CreateOrderCommand CreateOrderCommand { get; set; }
     public ObservableCollection&lt;Item&gt; Data { get; set; }
  
   public OrderViewModel()
      {
          this.CreateOrderCommand = new CreateOrderCommand(this);
          DataObservableCollection data= new DataObservableCollection();
          Data = data;
      }
  }
  </code></pre>
  
  <p>And I populate my buttons like this</p>
  
  <pre><code>&lt;WrapPanel x:Name="OrderButtons"&gt;
              &lt;ItemsControl   ItemTemplate="{StaticResource ButtonTemplate}"
                              ItemsSource="{Binding Data, Source={StaticResource OrderViewModel}}"&gt;
                  &lt;ItemsControl.ItemsPanel&gt;
                      &lt;ItemsPanelTemplate&gt;
                          &lt;WrapPanel Orientation="Horizontal"&gt;
                          &lt;/WrapPanel&gt;
                      &lt;/ItemsPanelTemplate&gt;
                  &lt;/ItemsControl.ItemsPanel&gt;
              &lt;/ItemsControl&gt;
      &lt;/WrapPanel&gt;
  </code></pre>