Getting WPF Data Grid Context Menu Click Row Getting WPF Data Grid Context Menu Click Row wpf wpf

Getting WPF Data Grid Context Menu Click Row


So based on your example code, I presume you bind your DataGrid to an ObservableCollection of objects of which you bind the properties Site and Subject to the DataGridColumns.

Essentially, all you need to do is figure out what the item bound to the clicked DataGridRow is and remove that from your ObservableCollection. Here is some example code to get you started:

private void Context_Delete(object sender, RoutedEventArgs e){    //Get the clicked MenuItem    var menuItem = (MenuItem)sender;    //Get the ContextMenu to which the menuItem belongs    var contextMenu = (ContextMenu)menuItem.Parent;    //Find the placementTarget    var item = (DataGrid)contextMenu.PlacementTarget;    //Get the underlying item, that you cast to your object that is bound    //to the DataGrid (and has subject and state as property)    var toDeleteFromBindedList = (YourObject)item.SelectedCells[0].Item;    //Remove the toDeleteFromBindedList object from your ObservableCollection    yourObservableCollection.Remove(toDeleteFromBindedList);}


Typically, you do not deal with rows (if you do - think again about the reasons) - instead you work with view model. When you open context menu, you get your item selected, so it can be accessed via the DataGrid.SelectedItem property. However, if you really need DataGridRow - you have your DataGrid.SelectedIndex and there is a lot of answers here on SO on how to get the row. like Get row in datagrid


To expand morincer's point above with an example, I ended up with a simpler approach...

 private void MenuItem_OnClickRemoveSource(object sender, RoutedEventArgs e) {     if (SourceDataGrid.SelectedItem == null) return;  //safety first     _importViewModel.SourceList.Remove((SourceFileInfo)SourceDataGrid.SelectedItem); }

In my case, the

_importViewModel.SourceList 

is the ObservableCollection the rows are bound to. So per best practices, I simple remove the selected item from the collection and the binding takes care of the UI.