Skip to content

Instantly share code, notes, and snippets.

@rmnblm
Created February 3, 2016 19:43
Show Gist options
  • Save rmnblm/2bccb88463ade0342cd6 to your computer and use it in GitHub Desktop.
Save rmnblm/2bccb88463ade0342cd6 to your computer and use it in GitHub Desktop.
using System;
using System.Windows.Input;
/// <summary>
/// A command whose sole purpose is to relay its functionality to
/// other objects by invoking delegates.
/// The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
private readonly Action execute;
private readonly Func<bool> canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action execute)
: this(execute, null)
{
}
public RelayCommand(Action execute,
Func<bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (canExecute == null)
{
return true;
}
return canExecute();
}
public void Execute(object parameter)
{
execute();
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment