Delegate Commands

  • 3 minutes to read

Delegate commands are an implementation of the Microsoft.UI.Xaml.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.

Creating DelegateCommands

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

public class DelegateCommandsViewModel : ViewModelBase {
    public IMessageBoxService MessageBoxService { get { return GetService<IMessageBoxService>(); } }
    public DelegateCommand DelegateCommand1 { get; private set; }
    public DelegateCommand<string> DelegateCommand2 { get; private set; }

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

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

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 parameterized type.

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

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

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

Updating Commands

Call the RaiseCanExecuteChanged method to update your commands.

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

    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();
    }
}