Table of Contents

Class RelayCommand<T>

Namespace
Wpf.Ui.Input
Assembly
Wpf.Ui.dll

A generic command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is true. This class allows you to accept command parameters in the Execute(T?) and CanExecute(T?) callback methods.

public class RelayCommand<T> : IRelayCommand<T>, IRelayCommand, ICommand

Type Parameters

T

The type of parameter being passed as input to the callbacks.

Inheritance
RelayCommand<T>
Implements
Inherited Members

Constructors

RelayCommand(Action<T?>)

Initializes a new instance of the RelayCommand<T> class that can always execute.

public RelayCommand(Action<T?> execute)

Parameters

execute Action<T>

The execution logic.

Remarks

Due to the fact that the ICommand interface exposes methods that accept a nullable object parameter, it is recommended that if T is a reference type, you should always declare it as nullable, and to always perform checks within execute.

Exceptions

ArgumentNullException

Thrown if execute is null.

RelayCommand(Action<T?>, Predicate<T?>)

Initializes a new instance of the RelayCommand<T> class.

public RelayCommand(Action<T?> execute, Predicate<T?> canExecute)

Parameters

execute Action<T>

The execution logic.

canExecute Predicate<T>

The execution status logic.

Remarks

Due to the fact that the ICommand interface exposes methods that accept a nullable object parameter, it is recommended that if T is a reference type, you should always declare it as nullable, and to always perform checks within execute.

Exceptions

ArgumentNullException

Thrown if execute or canExecute are null.

Methods

CanExecute(object?)

Defines the method that determines whether the command can execute in its current state.

public bool CanExecute(object? parameter)

Parameters

parameter object

Data used by the command. If the command does not require data to be passed, this object can be set to null.

Returns

bool

true if this command can be executed; otherwise, false.

CanExecute(T?)

Provides a strongly-typed variant of CanExecute(object).

public bool CanExecute(T? parameter)

Parameters

parameter T

The input parameter.

Returns

bool

Whether or not the current command can be executed.

Remarks

Use this overload to avoid boxing, if T is a value type.

Execute(object?)

Defines the method to be called when the command is invoked.

public void Execute(object? parameter)

Parameters

parameter object

Data used by the command. If the command does not require data to be passed, this object can be set to null.

Execute(T?)

Provides a strongly-typed variant of Execute(object).

public void Execute(T? parameter)

Parameters

parameter T

The input parameter.

Remarks

Use this overload to avoid boxing, if T is a value type.

NotifyCanExecuteChanged()

Notifies that the CanExecute(object) property has changed.

public void NotifyCanExecuteChanged()

Events

CanExecuteChanged

Occurs when changes occur that affect whether or not the command should execute.

public event EventHandler? CanExecuteChanged

Event Type

EventHandler