DelegateCommand Class

Factory class for creating command instances that can be used with WPF data binding.

public static class DelegateCommand

Example

public class MyViewModel
{
    public IDelegateCommand SaveCommand { get; }

    public MyViewModel()
    {
        SaveCommand = DelegateCommand.Create(Save, CanSave);
    }

    private void Save()
    {
        // Save logic
    }

    private bool CanSave()
    {
        return true;
    }
}

Methods

NameDescription
Create(Action?)Creates a command that executes the specified action.
Create(Action?, Func<bool>?)Creates a command that executes the specified action and checks if it can execute.
Create(Action<object?>?)Creates a command that executes the specified action with a parameter.
Create(Action<object?>?, Func<object?, bool>?)Creates a command that executes the specified action with a parameter and checks if it can execute.
Create(Func<object?, Task>?)Creates an asynchronous command that executes the specified task with a parameter.
Create(Func<object?, Task>?, Func<object?, bool>?)Creates an asynchronous command that executes the specified task with a parameter and checks if it can execute.
Create(Func<Task>?)Creates an asynchronous command that executes the specified task.
Create(Func<Task>?, Func<bool>?)Creates an asynchronous command that executes the specified task and checks if it can execute.

Create overloads

Create(Action?)

DelegateCommand.cs

Creates a command that executes the specified action.

public static IDelegateCommand Create(Action? execute)
Parameters
execute Action?
The action to execute when the command is invoked.
Returns

IDelegateCommand A new IDelegateCommand instance.

Create(Action?, Func<bool>?)

DelegateCommand.cs

Creates a command that executes the specified action and checks if it can execute.

public static IDelegateCommand Create(Action? execute, Func<bool>? canExecute)
Parameters
execute Action?
The action to execute when the command is invoked.
canExecute Func<bool>?
The function that determines if the command can execute.
Returns

IDelegateCommand A new IDelegateCommand instance.

Create(Action<object?>?)

DelegateCommand.cs

Creates a command that executes the specified action with a parameter.

public static IDelegateCommand Create(Action<object?>? execute)
Parameters
execute Action<object?>?
The action to execute when the command is invoked.
Returns

IDelegateCommand A new IDelegateCommand instance.

Create(Action<object?>?, Func<object?, bool>?)

DelegateCommand.cs

Creates a command that executes the specified action with a parameter and checks if it can execute.

public static IDelegateCommand Create(Action<object?>? execute, Func<object?, bool>? canExecute)
Parameters
execute Action<object?>?
The action to execute when the command is invoked.
canExecute Func<object?, bool>?
The function that determines if the command can execute.
Returns

IDelegateCommand A new IDelegateCommand instance.

Create(Func<object?, Task>?)

DelegateCommand.cs

Creates an asynchronous command that executes the specified task with a parameter.

public static IDelegateCommand Create(Func<object?, Task>? execute)
Parameters
execute Func<object?, Task>?
The task to execute when the command is invoked.
Returns

IDelegateCommand A new IDelegateCommand instance.

Remarks

Execution is fire-and-forget: if the task faults, the exception is rethrown on the dispatcher and cannot be caught by the caller. Handle it inside execute, or subscribe to Application.DispatcherUnhandledException. The command reports ICommand.CanExecute as false while the task runs.

Create(Func<object?, Task>?, Func<object?, bool>?)

DelegateCommand.cs

Creates an asynchronous command that executes the specified task with a parameter and checks if it can execute.

public static IDelegateCommand Create(Func<object?, Task>? execute, Func<object?, bool>? canExecute)
Parameters
execute Func<object?, Task>?
The task to execute when the command is invoked.
canExecute Func<object?, bool>?
The function that determines if the command can execute.
Returns

IDelegateCommand A new IDelegateCommand instance.

Remarks

Execution is fire-and-forget: if the task faults, the exception is rethrown on the dispatcher and cannot be caught by the caller. Handle it inside execute, or subscribe to Application.DispatcherUnhandledException. The command reports ICommand.CanExecute as false while the task runs, regardless of canExecute.

Create(Func<Task>?)

DelegateCommand.cs

Creates an asynchronous command that executes the specified task.

public static IDelegateCommand Create(Func<Task>? execute)
Parameters
execute Func<Task>?
The task to execute when the command is invoked.
Returns

IDelegateCommand A new IDelegateCommand instance.

Remarks

Execution is fire-and-forget: if the task faults, the exception is rethrown on the dispatcher and cannot be caught by the caller. Handle it inside execute, or subscribe to Application.DispatcherUnhandledException. The command reports ICommand.CanExecute as false while the task runs.

Create(Func<Task>?, Func<bool>?)

DelegateCommand.cs

Creates an asynchronous command that executes the specified task and checks if it can execute.

public static IDelegateCommand Create(Func<Task>? execute, Func<bool>? canExecute)
Parameters
execute Func<Task>?
The task to execute when the command is invoked.
canExecute Func<bool>?
The function that determines if the command can execute.
Returns

IDelegateCommand A new IDelegateCommand instance.

Remarks

Execution is fire-and-forget: if the task faults, the exception is rethrown on the dispatcher and cannot be caught by the caller. Handle it inside execute, or subscribe to Application.DispatcherUnhandledException. The command reports ICommand.CanExecute as false while the task runs, regardless of canExecute.