Skip to content

Instantly share code, notes, and snippets.

@somapatrik
Created November 20, 2023 10:20
Show Gist options
  • Select an option

  • Save somapatrik/28646a9c577b67ccf5432c2d91a3a300 to your computer and use it in GitHub Desktop.

Select an option

Save somapatrik/28646a9c577b67ccf5432c2d91a3a300 to your computer and use it in GitHub Desktop.
WPF Relay Command
public class RelayCommand : ICommand
{
private Action<object> _execute;
private Func<object, bool> _canExecute;
public RelayCommand(Action<object> execute)
{
_execute = execute;
_canExecute = null;
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment