title: How to delete selected row from a datagridwith context menu in Silverlight?
body: <p>I need to be able to delete a selected row from a datagrid with "Delete” context menu item. I have hard time finding the path to the selected row. I am wondering if someone can look at the code below and let me know what will be the appropriate to do within my code. Thank you in advance.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;UserControl.DataContext&gt;     
      &lt;local:MainPage_ViewModel/&gt; 
  &lt;/UserControl.DataContext&gt;  
  &lt;StackPanel Orientation="Horizontal"&gt;  
  
      &lt;data:DataGrid ItemsSource="{Binding Coordinates}" AutoGenerateColumns="False" Margin="10"&gt;
          &lt;!-- ContextMenu --&gt;
            &lt;toolkit:ContextMenuService.ContextMenu&gt;
               &lt;toolkit:ContextMenu x:Name="Menu" &gt;
                  &lt;toolkit:MenuItem x:Name="Edit" /&gt;
                  &lt;toolkit:MenuItem x:Name="Delete" Tag="{Binding}" Click="Button_Click" /&gt;
              &lt;/toolkit:ContextMenu&gt;
          &lt;/toolkit:ContextMenuService.ContextMenu&gt;      
          &lt;data:DataGrid.Columns&gt;             
              &lt;data:DataGridTextColumn Header="X Position" Width="100" Binding="{Binding X}"/&gt;             
              &lt;data:DataGridTextColumn Header="Y Position" Width="100" Binding="{Binding Y}"/&gt;             
              &lt;data:DataGridTemplateColumn Header="Delete Item" Width="100"&gt;                 
                  &lt;data:DataGridTemplateColumn.CellTemplate&gt;                     
                      &lt;DataTemplate&gt;                         
                          &lt;Button Content="Delete" Tag="{Binding}" Click="Button_Click"/&gt;                     
                      &lt;/DataTemplate&gt;                 
                  &lt;/data:DataGridTemplateColumn.CellTemplate&gt;             
              &lt;/data:DataGridTemplateColumn&gt;         
          &lt;/data:DataGrid.Columns&gt;     
      &lt;/data:DataGrid&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>public partial class MainPage : UserControl
  {
      public MainPage()
      {
          InitializeComponent();         
          this.viewModel = this.DataContext as MainPage_ViewModel; 
      }
  
      private MainPage_ViewModel viewModel;      
      private void Button_Click(object sender, RoutedEventArgs e)     
      {         
          viewModel.DeleteCoordinate((sender as Button).Tag as Coordinate_DataViewModel);     
      }
  }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>public class MainPage_ViewModel : INotifyPropertyChanged 
  {
      public MainPage_ViewModel()
      {
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));         
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));         
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));         
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));         
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));         
          coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
      }
  
      public ObservableCollection&lt;Coordinate_DataViewModel&gt; Coordinates     
      {         
          get { return coordinates; }         
          set          
          {             
              if (coordinates != value)             
              {                 
                  coordinates = value;                 
                  OnPropertyChanged("Coordinates");             
              }         
          }     
      }     
      private ObservableCollection&lt;Coordinate_DataViewModel&gt; coordinates = new ObservableCollection&lt;Coordinate_DataViewModel&gt;();      
      public event PropertyChangedEventHandler PropertyChanged;      
  
      public void OnPropertyChanged(string propertyName)     
      {         
          if (PropertyChanged != null)         
          {             
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));         
          }     
      }      
  
      public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)     
      {         
          coordinates.Remove(dvmToDelete);     
      }
  
      public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
      {
  
      }
  }
  
  //Model
  public class Coordinate_Model 
  {     
      public double X;     
      public double Y; 
  } 
  
  //DataViewModel
  public class Coordinate_DataViewModel 
  {     
      public Coordinate_DataViewModel(Coordinate_Model model)     
      {         
          this.underlyingModel = model;     
      }     
  
      private Coordinate_Model underlyingModel;      
      public double X     
      {         
          get { return underlyingModel.X; }         
          set { underlyingModel.X = value; }     
      }      
  
      public double Y     
      {         
          get { return underlyingModel.Y; }         
          set { underlyingModel.Y = value; }     
      }      public string XYCoordinate     
  
      {         
          get { return "(" + X + "," + Y + ")"; }     
      } 
  }
  </code></pre>