Skip to main content

DelegateCommand Class

A delegate command that implements the ICommand interface and calls your parameterless delegates when Execute and CanExecute logic is invoked on the command.

Namespace: DevExpress.Mvvm

Assembly: DevExpress.WinUI.Mvvm.v23.2.dll

NuGet Package: DevExpress.WinUI

Declaration

public class DelegateCommand :
    DelegateCommand<object>

The following members return DelegateCommand objects:

Remarks

Delegate commands implement the ICommand interface and allow you to create commands in a ViewModel. A delegate command calls methods (delegates) that you assign to the command when the command’s Execute and CanExecute logic is invoked.

Run Demo: DelegateCommand Module in the WinUI MVVM Demo

DevExpress WinUI MVVM Framework also includes the DelegateCommand<T> class that is the parameterized version of delegate commands.

Create Delegate Commands

The DelegateCommand<T> and DelegateCommand classes contain constructors that accept Execute delegate and optional CanExecute delegate parameters.

The following code sample creates delegate commands that call the MessageBoxService.ShowAsync method:

public class DelegateCommandsViewModel : ViewModelBase {
    public IMessageBoxService MessageBoxService => GetUIService<IMessageBoxService>();
    public DelegateCommand DelegateCommand1 { get; }
    public DelegateCommand<string> DelegateCommand2 { get; }

    public DelegateCommandsViewModel() {
        DelegateCommand1 = new DelegateCommand(
            execute: () => MessageBoxService.ShowAsync("This is a DelegateCommand"));

        DelegateCommand2 = new DelegateCommand<string>(
            execute: x => MessageBoxService.ShowAsync(x),
            canExecute: x => !string.IsNullOrEmpty(x));
    }
}

Convert Command Parameters

The DelegateCommand<T> class converts the command argument to the parameterized type if possible.

The following code snippet converts the CommandParameter‘s String value to the type of the DocumentType:

<Button Command="{x:Bind ShowDocumentCommand}" CommandParameter="Text"/>
public enum DocumentType { Text, Data }

public class DelegateCommandsViewModel : ViewModelBase {
    public DelegateCommand<DocumentType> ShowDocumentCommand { get; }

    public DelegateCommandsViewModel() {
        ShowDocumentCommand = new DelegateCommand<DocumentType>(ShowDocument);
    }
    void ShowDocument(DocumentType parameter) {
        if(parameter == DocumentType.Text) {
            //... 
        }
        if(parameter == DocumentType.Data) {
            //... 
        }
    }
}

Update Commands

Call the RaiseCanExecuteChanged method to update your commands.

public class DelegateCommandsViewModel : ViewModelBase {
    public DelegateCommand GoBackCommand{ get; }
    public DelegateCommand GoForwardCommand { get; }

    public DelegateCommandsViewModel() {
        GoBackCommand = new DelegateCommand(GoBack, CanGoBack);
        GoForwardCommand = new DelegateCommand(GoForward, CanGoForward);
    }

    void GoBack() {
        //... 
        UpdateCommandsState();
    }
    void GoForward() {
        //... 
        UpdateCommandsState();
    }
    bool CanGoBack() {
        //... 
    }
    bool CanGoForward() {
        //... 
    }
    void UpdateCommandsState() {
        GoBackCommand.RaiseCanExecuteChanged();
        GoForwardCommand.RaiseCanExecuteChanged();
    }
}

Inheritance

See Also