Edit this page

DelegateCommand

The DelegateCommand class provides a simple ICommand implementation.

It exposes the following methods and events:

  • CanExecute: Defines the method that determines whether the command can execute in its current state.
  • Execute: Defines the method to be called when the command is invoked.
  • InvalidateCanExecute: Raises the CanExecuteChanged event.
  • CanExecuteChanged: Raised when changes occur that affect whether the command should execute.

The DelegateCommand constructor has two overloads. The first accepts just a Delegate to execute as a parameter. The second one accepts the Delegate to execute as well as a Predicate that allows/bans the execution.

[C#] Example 1: DelegateCommand implementation in your ViewModel that accepts a delegate:

    public ICommand CustomCommand { get; set; }

    public MyViewModel()
    {
        this.CustomCommand = new DelegateCommand(onCustomCommandExecuted);
    }

    private void onCustomCommandExecuted(object obj)
    {
        MessageBox.Show("Custom Command Executed!");
    }

[C#] Example 2: DelegateCommand implementation in your ViewModel that accepts a delegate and a predicate:

    public bool CanExecuteCommand { get; set; }
    public ICommand CustomCommand { get; set; }

    public MyViewModel()
    {
        this.CustomCommand = new DelegateCommand(onCustomCommandExecuted, canBeExecuted);
    }

    private bool canBeExecuted(object obj)
    {
        return this.CanExecuteCommand;
    }

    private void onCustomCommandExecuted(object obj)
    {
        MessageBox.Show("Custom Command Executed!");
    }

See Also