Created
May 9, 2012 21:17
-
-
Save schuster-rainer/2648922 to your computer and use it in GitHub Desktop.
Implementation from Josh Smith of the RelayCommand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RelayCommand _saveCommand; | |
public ICommand SaveCommand | |
{ | |
get | |
{ | |
if (_saveCommand == null) | |
{ | |
_saveCommand = new RelayCommand(param => this.Save(), | |
param => this.CanSave ); | |
} | |
return _saveCommand; | |
} | |
} |
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
}
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()) { }
}
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.
This is cool guys, thanks for sharing.
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
How about this?