Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

45 righe
1.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. namespace Example.ViewModels
  8. {
  9. public class ViewModelCommand : ICommand
  10. {
  11. //Fields
  12. private readonly Action<object> _executeAction;
  13. private readonly Predicate<object> _canExecuteAction;
  14. public ViewModelCommand(Action<object> executeAction)
  15. {
  16. _executeAction = executeAction;
  17. _canExecuteAction = null;
  18. }
  19. public ViewModelCommand(Action<object> executeAction, Predicate<object> canExecuteAction)
  20. {
  21. _executeAction = executeAction;
  22. _canExecuteAction = canExecuteAction;
  23. }
  24. public event EventHandler CanExecuteChanged
  25. {
  26. add { CommandManager.RequerySuggested += value; }
  27. remove { CommandManager.RequerySuggested -= value; }
  28. }
  29. //Methods
  30. public bool CanExecute(object parameter)
  31. {
  32. return _canExecuteAction == null ? true : _canExecuteAction(parameter);
  33. }
  34. public void Execute(object parameter)
  35. {
  36. _executeAction(parameter);
  37. }
  38. }
  39. }