SHARE
TWEET

Untitled

a guest Jun 19th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region File Header
  2. /********************************************************
  3.  *
  4.  *  $Id: RelayCommand.cs 99 2010-07-07 18:35:13Z jeff $
  5.  *  
  6.  *  $Author: jeff $
  7.  *  $Date: 2010-07-07 11:35:13 -0700 (Wed, 07 Jul 2010) $
  8.  *  $Revision: 99 $
  9.  *  
  10.  *  $LastChangedBy: jeff $
  11.  *  $LastChangedDate: 2010-07-07 11:35:13 -0700 (Wed, 07 Jul 2010) $
  12.  *  $LastChangedRevision: 99 $
  13.  *  
  14.  *  (C) Copyright 2009 Jeff Boulanger
  15.  *  All rights reserved.
  16.  *  
  17.  ********************************************************/
  18. #endregion
  19.  
  20. using System;
  21. using System.Windows.Input;
  22. using System.Windows.Threading;
  23. using System.Diagnostics;
  24. using System.Threading;
  25.  
  26. namespace Fds.PresentationFramework.Input
  27. {
  28.     /// <summary>
  29.     ///
  30.     /// </summary>
  31.     /// <typeparam name="T"></typeparam>
  32.     public class RelayCommand<T> : ICommand
  33.     {
  34.         private Action<T> _execute;
  35.         private Predicate<T> _canExecute;
  36.  
  37.         /// <summary>
  38.         /// Occurs when changes occur that affect whether the command should execute.
  39.         /// </summary>
  40.         public event EventHandler CanExecuteChanged;
  41.  
  42.         /// <summary>
  43.         /// Creates a new instance of RelayCommand
  44.         /// </summary>
  45.         /// <param name="execute">The action to execute when the command is invoked</param>
  46.         public RelayCommand(Action<T> execute)
  47.         {
  48.             _execute = execute;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Creates a new instance of RelayCommand
  53.         /// </summary>
  54.         /// <param name="execute">The action to execute when the command is invoked</param>
  55.         /// <param name="canExecute">The command to execute that will return a boolean if the command can or cannot be executed</param>
  56.         public RelayCommand(Action<T> execute, Predicate<T> canExecute)
  57.         {
  58.             _execute = execute;
  59.             _canExecute = canExecute;
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Defines the method that determines whether the command can execute in its current state.
  64.         /// </summary>
  65.         /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
  66.         /// <returns>true if this command can be executed; otherwise, false.</returns>
  67.         public bool CanExecute(object parameter)
  68.         {
  69.             if (_canExecute != null)
  70.             {
  71.                 return _canExecute((T)parameter);
  72.             }
  73.  
  74.             return true;
  75.         }
  76.  
  77.         /// <summary>
  78.         /// Defines the method to be called when the command is invoked.
  79.         /// </summary>
  80.         /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
  81.         public void Execute(object parameter)
  82.         {
  83.                 _execute((T)parameter);
  84.                 OnCanExecuteChanged(this, EventArgs.Empty);
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Notifies the CanExecuteChanged event handler that the CanExecute value should be changed.  This fires when Execute is invoked.
  89.         /// </summary>
  90.         /// <param name="sender"></param>
  91.         /// <param name="e"></param>
  92.         protected virtual void OnCanExecuteChanged(object sender, EventArgs e)
  93.         {
  94.             if (CanExecuteChanged != null)
  95.             {
  96.                 CanExecuteChanged(sender, e);
  97.             }
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Forces the command to refresh its CanExecute() state.
  102.         /// </summary>
  103.         public void RaiseCanExecuteChanged()
  104.         {
  105.             OnCanExecuteChanged(this, EventArgs.Empty);
  106.         }
  107.     }
  108. }
RAW Paste Data
Top