Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (70 sloc) 2.17 KB
using System;
namespace Template10.Mvvm
{
// http://codepaste.net/jgxazh
using System.Diagnostics;
public class Command : System.Windows.Input.ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action execute, Func<bool> canexecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canexecute ?? (() => true);
}
[DebuggerStepThrough]
public bool CanExecute(object p)
{
try { return _canExecute(); }
catch { return false; }
}
public void Execute(object p)
{
if (!CanExecute(p))
return;
try { _execute(); }
catch { Debugger.Break(); }
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
public class Command<T> : System.Windows.Input.ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action<T> execute, Func<T, bool> canexecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canexecute ?? (e => true);
}
[DebuggerStepThrough]
public bool CanExecute(object p)
{
try
{
var _Value = (T)Convert.ChangeType(p, typeof(T));
return _canExecute == null ? true : _canExecute(_Value);
}
catch { return false; }
}
public void Execute(object p)
{
if (!CanExecute(p))
return;
var _Value = (T)Convert.ChangeType(p, typeof(T));
_execute(_Value);
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
}