Created
January 14, 2015 12:01
-
-
Save Vanlalhriata/3ab7d2702751bba6dcce to your computer and use it in GitHub Desktop.
DelegateCommand for MVVM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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