Friday, March 2, 2012

DelegateCommand in Silverlight

public class DelegateCommand : ICommand
{
    /// <summary>
    /// Occurs when changes occur that affect whether the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged;
 
    Func<objectbool> canExecute;
    Action<object> executeAction;
    bool canExecuteCache;
 
    /// <summary>
    /// Initializes a new instance of the <see cref="DelegateCommand"/> class.
    /// </summary>
    /// <param name="executeAction">The execute action.</param>
    /// <param name="canExecute">The can execute.</param>
    public DelegateCommand(Action<object> executeAction,
                            Func<objectbool> canExecute)
    {
        this.executeAction = executeAction;
        this.canExecute = canExecute;
    }
 
    /// <summary>
    /// Permet de déclencher l'evenement de modification du CanExecute
    /// </summary>
    public void OnCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
            CanExecuteChanged(thisnew EventArgs());
    }
 
    #region ICommand Members
    /// <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)
    {
        bool tempCanExecute = canExecute(parameter);
 
        if (canExecuteCache != tempCanExecute)
        {
            canExecuteCache = tempCanExecute;
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(thisnew EventArgs());
            }
        }
 
        return canExecuteCache;
    }
 
    /// <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)
    {
        executeAction(parameter);
    }
    #endregion
} 

Source : http://compositewpf.codeplex.com/

No comments:

Post a Comment