RelayCommand.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Input;
  15. namespace Xceed.Wpf.AvalonDock.Commands
  16. {
  17. internal class RelayCommand : ICommand
  18. {
  19. #region Fields
  20. readonly Action<object> _execute;
  21. readonly Predicate<object> _canExecute;
  22. #endregion // Fields
  23. #region Constructors
  24. public RelayCommand(Action<object> execute)
  25. : this(execute, null)
  26. {
  27. }
  28. public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  29. {
  30. if (execute == null)
  31. throw new ArgumentNullException("execute");
  32. _execute = execute;
  33. _canExecute = canExecute;
  34. }
  35. #endregion // Constructors
  36. #region ICommand Members
  37. public bool CanExecute(object parameter)
  38. {
  39. return _canExecute == null ? true : _canExecute(parameter);
  40. }
  41. public event EventHandler CanExecuteChanged
  42. {
  43. add { CommandManager.RequerySuggested += value; }
  44. remove { CommandManager.RequerySuggested -= value; }
  45. }
  46. public void Execute(object parameter)
  47. {
  48. _execute(parameter);
  49. }
  50. #endregion // ICommand Members
  51. }
  52. }