Skip to content

Instantly share code, notes, and snippets.

@schuster-rainer
Created May 9, 2012 21:17
Show Gist options
  • Save schuster-rainer/2648922 to your computer and use it in GitHub Desktop.
Save schuster-rainer/2648922 to your computer and use it in GitHub Desktop.
Implementation from Josh Smith of the RelayCommand
//http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave );
}
return _saveCommand;
}
}
@VijayChavda
Copy link

VijayChavda commented Jun 4, 2017

How about this?

        RelayCommand _saveCommand;
        public ICommand SaveCommand
        {
            get => _saveCommand ?? (_saveCommand = new RelayCommand(param => this.Save(), param => this.CanSave()));
        }

@AnkitBajpaii
Copy link

Make it as generic class:


public class RelayCommand<T> : ICommand
    {
        #region Fields

        readonly Action<T> _execute;
        readonly Predicate<T> _canExecute;

        #endregion // Fields

        #region Constructors

        public RelayCommand(Action<T> execute)
        : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion // Constructors

        #region ICommand Members

        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

        #endregion // ICommand Members
    }

@heltonbiker
Copy link

You can even have a non-generic class deriving from @AnkitBajpaii generic one:

public class RelayCommand : RelayCommand<object>
{
	public RelayCommand(Action execute)
		: this(execute, null) { }

	public RelayCommand(Action execute, Func<bool> canExecute)
		: base(param => execute(), param => canExecute()) { }
}

@mcsantiago
Copy link

mcsantiago commented Mar 24, 2018

Just out of curiosity, where does the CommandManager come from?

EDIT:
I found out why I didn't have the CommandManager. You have to make sure you include PresentationCore in your references.

@xtremertx
Copy link

This is cool guys, thanks for sharing.

@antikmozib
Copy link

You can even have a non-generic class deriving from @AnkitBajpaii generic one:

public class RelayCommand : RelayCommand<object>
{
	public RelayCommand(Action execute)
		: this(execute, null) { }

	public RelayCommand(Action execute, Func<bool> canExecute)
		: base(param => execute(), param => canExecute()) { }
}

This throws a NullReferenceException when canExecute is null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment