Getting WPF Data Grid Context Menu Click Row

后端 未结 6 565
无人共我
无人共我 2021-01-31 10:30

I have a WPF DataGrid



    

        
6条回答
  •  遇见更好的自我
    2021-01-31 10:58

    The accepted answer from dsfgsho makes sense but when using CommandBinding for the standard ApplicationCommands rather than an explicit Click event it is a little different as the sender is not the MenuItem but the DataGrid itself.

    XAML:

    
        
        
        
        
        
    
    
        
            
            
            
            
            
            
            
        
    
    

    Code Behind:

    private void DataGrid_Delete(object sender, ExecutedRoutedEventArgs e)
    {
        // Test whether cleared, resolved, etc., and confirm deletion
        var datagrid = (DataGrid)sender;
        var trans = (DataClasses.BankTransaction)datagrid.SelectedCells[0].Item;
    
        // Take action here; e.g., remove it from the underlying collection, remove it
        // from the DB, etc.
    
        e.Handled = true;
    }
    
    private void DataGrid_CanDelete(object sender, CanExecuteRoutedEventArgs e)
    {
         e.CanExecute = true;
         e.Handled = true;
    }
    

提交回复
热议问题