view SilverlightGlimpse/SilverlightValidation/Commands/RelayCommand.cs @ 73:d06c852e0167

looking good!
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 19:15:52 +0100
parents a0bcd783e612
children 86ed4919b126
line wrap: on
line source

using System;
using System.Windows.Input;

namespace SilverlightValidation.Commands
{
    public class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged = delegate { };

        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        public RelayCommand(Action<object> execute,
                            Predicate<object> canExecute = null)
        {
            if (execute == null) throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }


        public void UpdateCanExecuteCommand()
        {
            CanExecuteChanged(this, new EventArgs());
        }


        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute(parameter);
        }


        public void Execute(object parameter)
        {
            _execute(parameter);
        }
    }
}