Created
September 10, 2024 04:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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