Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Created April 17, 2013 23:14
Show Gist options
  • Save thomaslevesque/5408535 to your computer and use it in GitHub Desktop.
Save thomaslevesque/5408535 to your computer and use it in GitHub Desktop.
Basic DelegateCommand implementation
using System;
using System.Windows.Input;
namespace QuestionableContent.Input
{
public class DelegateCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public DelegateCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute();
}
public void Execute(object parameter)
{
_execute();
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment