PageRenderTime 85ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/SpiritMVVM/RelayCommand.Generic.cs

http://spiritmvvm.codeplex.com
C# | 107 lines | 54 code | 17 blank | 36 comment | 8 complexity | 70f72540a90f2ea99eb63279cebaf40a MD5 | raw file
  1. using System;
  2. using System.Windows.Input;
  3. namespace SpiritMVVM
  4. {
  5. /// <summary>
  6. /// The <see cref="RelayCommand{T}"/> class assigns an <see cref="Action{T}"/> delegate to the <see cref="Execute"/>
  7. /// method and a <see cref="Func{T, T}"/> delegate to the <see cref="CanExecute"/> method,
  8. /// allowing <see cref="ICommand"/> objects to be implemented completely from within a View-Model.
  9. /// </summary>
  10. /// <typeparam name="TParam">
  11. /// Determines the parameter type that will be passed in to your
  12. /// <see cref="Action{T}"/> and <see cref="Func{T, T}"/> delegates.
  13. /// </typeparam>
  14. public class RelayCommand<TParam> : ICommand, IRaiseCanExecuteChanged
  15. {
  16. #region Private Fields
  17. private readonly Func<TParam, bool> _canExecute;
  18. private readonly Action<TParam> _execute;
  19. #endregion Private Fields
  20. #region Constructors
  21. /// <summary>
  22. /// Create a new <see cref="RelayCommand"/> with the given execution delegate.
  23. /// </summary>
  24. /// <param name="execute">The <see cref="Action"/> to execute when the
  25. /// <see cref="Execute"/> method is called.</param>
  26. public RelayCommand(Action<TParam> execute)
  27. : this(execute, null)
  28. { }
  29. /// <summary>
  30. /// Create a new <see cref="RelayCommand"/> with the given execution delegate.
  31. /// </summary>
  32. /// <param name="execute">The <see cref="Action"/> to execute when the
  33. /// <see cref="Execute"/> method is called.</param>
  34. /// <param name="canExecute">The <see cref="Func{T, T}"/> to execute
  35. /// when the <see cref="CanExecute"/> method is queried.</param>
  36. public RelayCommand(Action<TParam> execute, Func<TParam, bool> canExecute)
  37. {
  38. if (execute == null)
  39. throw new ArgumentNullException("execute");
  40. _execute = execute;
  41. _canExecute = canExecute;
  42. }
  43. #endregion Constructors
  44. #region ICommand Implementation
  45. /// <summary>
  46. /// Event which is raised when the command's ability to be executed changes.
  47. /// </summary>
  48. public event EventHandler CanExecuteChanged = null;
  49. /// <summary>
  50. /// Determine if the command can be executed in its current state.
  51. /// </summary>
  52. /// <param name="parameter">An optional parameter.</param>
  53. /// <returns>Returns True if the command can be executed. Otherwise, false.</returns>
  54. public bool CanExecute(object parameter)
  55. {
  56. var canExecuteHandler = _canExecute;
  57. if (canExecuteHandler != null)
  58. {
  59. return canExecuteHandler((TParam)parameter);
  60. }
  61. return true;
  62. }
  63. /// <summary>
  64. /// Execute the command's delegate method.
  65. /// </summary>
  66. /// <param name="parameter">An optional parameter.</param>
  67. public void Execute(object parameter)
  68. {
  69. var executeHandler = _execute;
  70. if (executeHandler != null)
  71. {
  72. executeHandler((TParam)parameter);
  73. }
  74. }
  75. #endregion ICommand Implementation
  76. #region IRaiseCanExecuteChanged Implementation
  77. /// <summary>
  78. /// Raise the <see cref="ICommand.CanExecuteChanged"/> event.
  79. /// </summary>
  80. public void RaiseCanExecuteChanged()
  81. {
  82. var handler = CanExecuteChanged;
  83. if (handler != null)
  84. {
  85. handler(this, EventArgs.Empty);
  86. }
  87. }
  88. #endregion IRaiseCanExecuteChanged Implementation
  89. }
  90. }