How to: Replace Built-In Command with a Custom Command

  • 3 min to read

This topic describes how to use the service substitution technique to replace the built-in SpreadsheetControl command with a custom command.

NOTE

A complete sample project is available in the DevExpress-Examples/wpf-spreadsheet-how-to-replace-standard-command-with-custom-command repository on GitHub.

The SpreadsheetControl uses the command factory service to create its commands. You can substitute the default command factory service with its descendant to create a custom command.

Follow the steps below to create and register a custom command.

  1. Create a custom command class inherited from the command you want to replace. Override the ExecuteCore method to specify actions that the new command should perform.

    The example below shows how to customize the standard DevExpress.XtraSpreadsheet.Commands.FormatClearContentsCommand command.

    public class CustomFormatClearContentsCommand : DevExpress.XtraSpreadsheet.Commands.FormatClearContentsCommand
    {
        public CustomFormatClearContentsCommand(ISpreadsheetControl control)
            : base(control)
        {
        }
    
        protected override void ExecuteCore()
        {
            // Display a confirmation dialog before removing the cell's content. 
            MessageBoxResult result = DXMessageBox.Show("Do you want to clear the cell content?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
                base.ExecuteCore();
        }
    }
    
  2. Create a class inherited from SpreadsheetCommandFactoryServiceWrapper to replace the default command service. In this class, override the SpreadsheetCommandFactoryServiceWrapper.CreateCommand method to replace the default FormatClearContents and FormatClearContentsContextMenuItem commands with the custom command.

    public class CustomService : SpreadsheetCommandFactoryServiceWrapper
    {
        public CustomService(ISpreadsheetCommandFactoryService service)
            : base(service)
        {
        }
        public SpreadsheetControl Control { get; set; }
    
        public override SpreadsheetCommand CreateCommand(SpreadsheetCommandId id)
        {
            if (id == SpreadsheetCommandId.FormatClearContents || id == SpreadsheetCommandId.FormatClearContentsContextMenuItem)
                return new CustomFormatClearContentsCommand(Control);
            return base.CreateCommand(id);
        }
    }
    
  3. Create a CustomService instance and use the SpreadsheetControl.ReplaceService<T> method to replace the default service with the custom service. Note that due to WPF Spreadsheet implementation specifics, it is necessary to replace the built-in service after the control is loaded.

    void spreadsheetControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(() => {
            SubstituteService();
        }), System.Windows.Threading.DispatcherPriority.Render);
    }
    
    private void SubstituteService()
    {
        ISpreadsheetCommandFactoryService service = (ISpreadsheetCommandFactoryService)spreadsheetControl.GetService(typeof(ISpreadsheetCommandFactoryService));
        CustomService customService = new CustomService(service);
        spreadsheetControl.ReplaceService<ISpreadsheetCommandFactoryService>(customService);
        customService.Control = spreadsheetControl;
    }
    

Run the application. Select any cell and press DELETE, or click Clear Contents on the ribbon or in the context menu. The SpreadsheetControl displays the message "Do you want to clear the cell content?". Click Yes to clear the cell, or select No to cancel the operation.

DXSpreadsheet_CustomClearCommand

See Also