Skip to content

Instantly share code, notes, and snippets.

@papinko
Created September 10, 2024 04:44
Show Gist options
  • Save papinko/9e561992009949993b55436139c8a878 to your computer and use it in GitHub Desktop.
Save papinko/9e561992009949993b55436139c8a878 to your computer and use it in GitHub Desktop.
The benefit of using relay command is that you can bind commands directly to the ViewModels.
using System;
using System.Windows.Input;
public class RelayCommand : ICommand
{
private Action<object> _execute;
private Func<object, bool> _canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
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