Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
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);
}
}
}