[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
  Prerequisites
 [Expand]What's Installed
 [Expand]Common Concepts
 [Collapse]MVVM Framework
  [Expand]ViewModels
  [Collapse]Commands
    Delegate Commands
    Asynchronous Commands
  [Expand]Behaviors
  [Expand]Services
  [Expand]DXBinding
  [Expand]MIF
   Converters
   ViewLocator
   Messenger
   Data Annotation Attributes
   LayoutTreeHelper
   Weak Event
 [Expand]Controls and Libraries
 [Expand]Scaffolding Wizard
 [Expand]Localization
  Redistribution and Deployment
  Get More Help
 [Expand]API Reference
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Delegate Commands

Delegate commands are an implementation of the System.Windows.Input.ICommand interface, so they can be used to create commands in a ViewModel. A delegate command calls methods (delegates) that you assigned to the command when the command's Execute and CanExecute logic is invoked.

The following delegate commands are available.

  • DelegateCommand<T> - Specifies a command whose Execute and CanExecute delegates accept a single parameter of type T.
  • DelegateCommand - Specifies a command whose Execute and CanExecute delegates do not have any parameters.

Expanded Creating DelegateCommands

Both commands support two constructors: a constructor that accepts an Execute delegate, and a constructor that accepts Execute and CanExecute delegates.

Expanded CommandParameter Conversion

A DelegateCommand<T> automatically converts the command argument to the parameterized type if possible. In the example below, the CommandParameter (the "Text" string) will be automatically converted to the DocumentType parametrized type.

Expanded Updating Commands

The DelegateCommand constructors also have an optional useCommandManager parameter. This parameter specifies whether a DelegateCommand uses the CommandManager to raise the CanExecuteChanged event. By default, useCommandManager is true, making it unnecessary to manually implement the disable/enable logic for your commands. Once you set the CanExecute delegate for the command, the delegate is automatically triggered when an end-user interacts with the UI. Note that when the CommandManager is used for this purpose, the CanExecute handler is called frequently, so avoid performing time-consuming operations in the delegate.

If you pass false as the last constructor argument, the CommandManager is not used. In this case, update your command by calling the RaiseCanExecuteChanged method.

Expanded DelegateCommands in POCO

In a POCO View Model, DelegateCommands can be automatically generated based on public methods (with one parameter or none) of your View Model.

For instance, for the following Save and CanSave methods...

...the following SaveCommand property will be automatically generated.

Expanded Example

This example demonstrates how to create commands with the DelegateCommand class.

Show Me

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E5165.

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