title: Get item of ObservableCollection that triggers button
body: <p>so I've been working on a smart home implementation in c# for quite a while now but I can't figure out how to inform the Command which executes something, in this case just a remove button as an example, about the item of the Observable that triggers it. 
  To implement the Collection I'm using a template which also contains a button so that each item of the Collection has a button by it's own.</p>
  
  <p><a href="https://i.stack.imgur.com/ky3Ve.png" rel="nofollow noreferrer">ObservableCollection with remove Button for each Item</a></p>
  
  <p>So as you see in there, I have a Remove Button for each Item that is in the ObservableCollection and if I press it I want the Item which contains the Button to be removed but <strong>without using something like selectedItem</strong> because in that case I need to selected the item first before using the button which is not what I really want because you can press the button without selecting the item/row.
  In later time the button will be exchanged for a different function which needs to know which item the button belongs to.</p>
  
  <p>xaml code:</p>
  
  <pre><code>&lt;Page x:Class="Smart_Home.switchPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:Smart_Home"
          mc:Ignorable="d"
          Title="switchPage" Height="490" Width="998" Background="#10636e" &gt;
    &lt;Page.Resources&gt;
      &lt;local:SwitchViewmodel x:Key="SwitchViewmodel" /&gt;
      &lt;DataTemplate x:Key="UserTemplate"&gt;
        &lt;StackPanel&gt;
          &lt;Grid&gt;
            &lt;Grid Width="970" Margin="0,0,0,0"&gt;
              &lt;Grid HorizontalAlignment="Left" VerticalAlignment="Center" Width="300"&gt;
                &lt;TextBlock Text="{Binding Path=schalterName}"/&gt;
              &lt;/Grid&gt;
              &lt;Grid HorizontalAlignment="Center" Width="380"&gt;
                &lt;Grid HorizontalAlignment="Left" VerticalAlignment="Center" Width="200"&gt;
                  &lt;TextBlock Text="{Binding Path=schalterRaum}"/&gt;
                &lt;/Grid&gt;
                &lt;Grid HorizontalAlignment="Right" VerticalAlignment="Center" Width="30"&gt;
                  &lt;TextBlock Text="{Binding Path=schalterNummer}"/&gt;
                &lt;/Grid&gt;
              &lt;/Grid&gt;
              &lt;Grid HorizontalAlignment="Right" VerticalAlignment="Center" Width="60"&gt;
                &lt;Button Content="Switch" Height="40" Command="{Binding RemoveSelectedSwitch, Source={StaticResource SwitchViewmodel}}" /&gt;
              &lt;/Grid&gt;
            &lt;/Grid&gt;
          &lt;/Grid&gt;
        &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
    &lt;/Page.Resources&gt;
  
    &lt;Grid x:Name="WindowSelection" HorizontalAlignment="Left" Height="490" Margin="0,0,0,0" VerticalAlignment="Top" Width="998"&gt;
      &lt;Grid Height="400" VerticalAlignment="Top" Grid.ColumnSpan="2"&gt;
        &lt;Grid x:Name="ListNameGrid" VerticalAlignment="Top" Height="30"&gt;
          &lt;Grid HorizontalAlignment="Left" Margin="10,0,0,0"&gt;
            &lt;Label Content="Name" FontSize="14" Foreground="White"/&gt;
          &lt;/Grid&gt;
          &lt;Grid HorizontalAlignment="Center" Width="400"&gt;
            &lt;Grid HorizontalAlignment="Left"&gt;
              &lt;Label Content="Room" FontSize="14" Foreground="White"/&gt;
            &lt;/Grid&gt;
            &lt;Grid HorizontalAlignment="Right"&gt;
              &lt;Label Content="Number" FontSize="14" Foreground="White"/&gt;
            &lt;/Grid&gt;
          &lt;/Grid&gt;
          &lt;Grid HorizontalAlignment="Right" Margin="0,0,10,0"&gt;
            &lt;Label Content="Status" FontSize="14" Foreground="White" /&gt;
          &lt;/Grid&gt;
        &lt;/Grid&gt;
        &lt;Grid x:Name="ListGrid" VerticalAlignment="Top" Margin="0,30,0,0"&gt;
          &lt;ListBox x:Name="Switches_lb" IsSynchronizedWithCurrentItem="true" FontSize="20" Foreground="White" ItemsSource="{Binding SwitchesList, Source={StaticResource SwitchViewmodel}}" ItemTemplate="{StaticResource UserTemplate}" Background="#10636e" BorderThickness="0" Margin="5,0,5,0"/&gt;
        &lt;/Grid&gt;
      &lt;/Grid&gt;
    &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>Viewmodel: </p>
  
  <pre><code>public class SwitchViewmodel
    {
      public int selectedCollection { get; }
      public ObservableCollection&lt;SwitchView&gt; SwitchesList { get; set; }
      public ICommand AddNewSwitch { get; set; }
      public ICommand RemoveSelectedSwitch { get; set; }
  
  
      public SwitchViewmodel()
      {
        AddNewSwitch = new Command(AddNewSwitchExecuteMethod, AddNewSwitchcanexecuteMethod);
        RemoveSelectedSwitch = new Command(RemoveSelectedSwitchExeCuteMethod, RemoveSelectedSwitchAddNewSwitchcanexecuteMethod);
  
        SwitchesList = new ObservableCollection&lt;SwitchView&gt;
        {
          new SwitchView() { schalterName = "Wandlampe", schalterRaum = "Wohnzimmer", schalterNummer = 1, schalterStatus = false },
          new SwitchView() { schalterName = "Deckenlampe", schalterRaum = "Küche", schalterNummer = 2, schalterStatus = false },
          new SwitchView() { schalterName = "Tischlampe", schalterRaum = "Wohnzimmer", schalterNummer = 39, schalterStatus = false },
        };
  
      }
  
      private bool RemoveSelectedSwitchAddNewSwitchcanexecuteMethod(object parameter)
      {
        return true;
      }
  
      private void RemoveSelectedSwitchExeCuteMethod(object parameter)
      {
        //In here remove method
      }
  
      private bool AddNewSwitchcanexecuteMethod(object parameter)
      {
        return true;
      }
  
      private void AddNewSwitchExecuteMethod(object parameter)
      {
        Console.WriteLine("Command test");
        Console.WriteLine("Count vorher: " + SwitchesList.Count());
        SwitchesList.Add(new SwitchView() { schalterName = "Wandlampe", schalterRaum = "Wohnzimmer", schalterNummer = 1, schalterStatus = false });
        Console.WriteLine("Count nachher: " + SwitchesList.Count());
      }
    }
  </code></pre>
  
  <p>Collection:</p>
  
  <pre><code>public class SwitchView
    {
      public string schalterName { get; set; }
      public string schalterRaum { get; set; }
      public int schalterNummer { get; set; }
      public Boolean schalterStatus { get; set; }
    }
  </code></pre>
  
  <p>Command:</p>
  
  <pre><code>  public class Command : ICommand
    {
      Action&lt;object&gt; executeMethod;
      Func&lt;object, bool&gt; canexecuteMethod;
  
      public Command(Action&lt;object&gt; executeMethod, Func&lt;object, bool&gt; canexecuteMethod)
      {
        this.executeMethod = executeMethod;
        this.canexecuteMethod = canexecuteMethod;
      }
  
      public bool CanExecute(object parameter)
      {
        return true;
      }
  
      public void Execute(object parameter)
      {
        executeMethod(parameter);
      }
  
      public event EventHandler CanExecuteChanged
      {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
      }
    }
  </code></pre>
  
  <p>So it would be really helpful if someone could help me out with my problem as I'm really new in using MVVM pattern in c# wpf.</p>
  
  <p>Thank you and have a nice day</p>