Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region File Header
- /********************************************************
- *
- * $Id: RelayCommand.cs 99 2010-07-07 18:35:13Z jeff $
- *
- * $Author: jeff $
- * $Date: 2010-07-07 11:35:13 -0700 (Wed, 07 Jul 2010) $
- * $Revision: 99 $
- *
- * $LastChangedBy: jeff $
- * $LastChangedDate: 2010-07-07 11:35:13 -0700 (Wed, 07 Jul 2010) $
- * $LastChangedRevision: 99 $
- *
- * (C) Copyright 2009 Jeff Boulanger
- * All rights reserved.
- *
- ********************************************************/
- #endregion
- using System;
- using System.Windows.Input;
- using System.Windows.Threading;
- using System.Diagnostics;
- using System.Threading;
- namespace Fds.PresentationFramework.Input
- {
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class RelayCommand<T> : ICommand
- {
- private Action<T> _execute;
- private Predicate<T> _canExecute;
- /// <summary>
- /// Occurs when changes occur that affect whether the command should execute.
- /// </summary>
- public event EventHandler CanExecuteChanged;
- /// <summary>
- /// Creates a new instance of RelayCommand
- /// </summary>
- /// <param name="execute">The action to execute when the command is invoked</param>
- public RelayCommand(Action<T> execute)
- {
- _execute = execute;
- }
- /// <summary>
- /// Creates a new instance of RelayCommand
- /// </summary>
- /// <param name="execute">The action to execute when the command is invoked</param>
- /// <param name="canExecute">The command to execute that will return a boolean if the command can or cannot be executed</param>
- public RelayCommand(Action<T> execute, Predicate<T> canExecute)
- {
- _execute = execute;
- _canExecute = canExecute;
- }
- /// <summary>
- /// Defines the method that determines whether the command can execute in its current state.
- /// </summary>
- /// <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>
- /// <returns>true if this command can be executed; otherwise, false.</returns>
- public bool CanExecute(object parameter)
- {
- if (_canExecute != null)
- {
- return _canExecute((T)parameter);
- }
- return true;
- }
- /// <summary>
- /// Defines the method to be called when the command is invoked.
- /// </summary>
- /// <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>
- public void Execute(object parameter)
- {
- _execute((T)parameter);
- OnCanExecuteChanged(this, EventArgs.Empty);
- }
- /// <summary>
- /// Notifies the CanExecuteChanged event handler that the CanExecute value should be changed. This fires when Execute is invoked.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected virtual void OnCanExecuteChanged(object sender, EventArgs e)
- {
- if (CanExecuteChanged != null)
- {
- CanExecuteChanged(sender, e);
- }
- }
- /// <summary>
- /// Forces the command to refresh its CanExecute() state.
- /// </summary>
- public void RaiseCanExecuteChanged()
- {
- OnCanExecuteChanged(this, EventArgs.Empty);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement