Skip to content

Instantly share code, notes, and snippets.

@Vanlalhriata
Created January 14, 2015 12:01
Show Gist options
  • Save Vanlalhriata/3ab7d2702751bba6dcce to your computer and use it in GitHub Desktop.
Save Vanlalhriata/3ab7d2702751bba6dcce to your computer and use it in GitHub Desktop.
DelegateCommand for MVVM
using System;
namespace CommandHelpers
{
public class DelegateCommand : System.Windows.Input.ICommand
{
#region Fields
private readonly Action<object> execute;
private readonly Func<object, bool> canExecute;
#endregion Fields
#region Initializaton
public DelegateCommand(Action<object> pExecute, Func<object, bool> pCanExecute)
{
execute = pExecute;
canExecute = pCanExecute;
}
public DelegateCommand(Action<object> pExecute)
: this(pExecute, null)
{
}
#endregion Initializaton
#region ICommand implementation
public bool CanExecute(object commandParameter)
{
return null == canExecute ? false : canExecute(commandParameter);
}
public event EventHandler CanExecuteChanged;
public void Execute(object commandParameter)
{
if (CanExecute(commandParameter))
execute(commandParameter);
}
#endregion ICommand implementation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment