Skip to content

Instantly share code, notes, and snippets.

@Tocchann
Created March 31, 2022 11:23
Show Gist options
  • Save Tocchann/d4a28480ec97d2af65d03388607938db to your computer and use it in GitHub Desktop.
Save Tocchann/d4a28480ec97d2af65d03388607938db to your computer and use it in GitHub Desktop.
ICommand のミニマムベースクラス
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace SimpleMVVM
{
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Predicate<object> canExecute;
public RelayCommand( Action<object> execute_ )
: this( execute_, null )
{
}
public RelayCommand( Action<object> execute_, Predicate<object> canExecute_ )
{
execute = execute_;
canExecute = canExecute_;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
[DebuggerStepThrough]
public bool CanExecute( object parameter )
{
return canExecute == null ? true : 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