Skip to content

Instantly share code, notes, and snippets.

@bronumski
Created June 12, 2014 09:58
Show Gist options
  • Select an option

  • Save bronumski/ed76e342ee8e404709df to your computer and use it in GitHub Desktop.

Select an option

Save bronumski/ed76e342ee8e404709df to your computer and use it in GitHub Desktop.
WPF Relay command
public class RelayCommand<T> : ICommand
{
private readonly Predicate<T> canExecute;
private readonly Action<T> execute;
public RelayCommand(Action<T> action)
: this(x => true, action)
{
execute = action;
}
public RelayCommand(Predicate<T> canExecute, Action<T> execute)
{
this.canExecute = canExecute;
this.execute = execute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return canExecute((T)parameter);
}
public void UpdateCanExecuteState()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
}
public void Execute(object parameter)
{
execute((T)parameter);
UpdateCanExecuteState();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment