[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Collapse]WPF Controls
 [Expand]What's Installed
 [Expand].NET Core 3 Support
 [Expand]Common Concepts
 [Expand]MVVM Framework
 [Collapse]Controls and Libraries
  [Expand]Data Grid
  [Expand]Ribbon, Bars and Menu
  [Expand]Charts Suite
  [Expand]Pivot Grid
    Product Structure
   [Expand]Getting Started
   [Expand]Spreadsheet Document
    Supported Formats
   [Expand]Cell Basics
    Defined Names
    Data Binding
    Data Validation
   [Expand]Data Presentation
   [Expand]Pivot Table Overview
   [Expand]Charts and Graphics
    Operation Restrictions
    Find and Replace
   [Expand]Mail Merge
   [Expand]Visual Elements
    Themes and Templates
    [Expand]Rows and Columns
    [Expand]Data Binding
    [Expand]Formatting Cells
    [Expand]Conditional Formatting
    [Expand]Group Data
    [Expand]Filter Data
    [Expand]Pivot Tables
    [Expand]Mail Merge
    [Expand]Cell Templates
      How to: Replace Built-In Command with a Custom Command
  [Expand]Rich Text Editor
  [Expand]Tree List
  [Expand]Gauge Controls
  [Expand]Map Control
  [Expand]Layout Management
  [Expand]Windows Modern UI
  [Expand]Data Editors
  [Expand]Navigation Controls
  [Expand]Spell Checker
  [Expand]Property Grid
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Gantt Control
  [Expand]Diagram Control
  [Expand]Windows and Utility Controls
   Dialogs, Notifications and Panels
  [Expand]Scheduler (legacy)
 [Expand]Scaffolding Wizard
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

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

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


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.

  2. Create a class inherited from SpreadsheetCommandFactoryServiceWrapper (see 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.

  3. Create a CustomService instance and use the DevExpress.Xpf.Spreadsheet.SpreadsheetControl.ReplaceService``1 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.

Expanded See Also

Is this topic helpful?​​​​​​​