wpf-120516-controls-and-libraries-spreadsheet-examples-commands-how-to-replace-built-in-command-with-a-custom-command.md
This topic describes how to use the service substitution technique to replace the built-in SpreadsheetControl command with a custom command.
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.
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.
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.
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.
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.
See Also