/ Published in: C#
This class can be found in this article on MSDN: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090051 It makes it possible to add WPF-commands to your ViewModel classes without the need for using nested classes for each command.
Expand |
Embed | Plain Text
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) _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 } // To use this class within your viewmodel class: RelayCommand _myCommand; public ICommand MyCommand { get { if (_myCommand == null) { p => this.CanDoMyCommand(p) ); } return _myCommand; } }
You need to login to post a comment.
