Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Last active July 5, 2017 14:15
Show Gist options
  • Save JokerMartini/64596194301c5a1ab61e to your computer and use it in GitHub Desktop.
Save JokerMartini/64596194301c5a1ab61e to your computer and use it in GitHub Desktop.
C# Relay Command classes
private ICommand deleteContactCommand;
public ICommand DeleteContactCommand
{
get
{
return deleteContactCommand ?? (deleteContactCommand = new RelayCommand<Contact>(ExecuteDeleteContact));
}
}
private ICommand deleteSelectedContactsCommand;
public ICommand DeleteSelectedContactsCommand
{
get
{
return deleteSelectedContactsCommand ?? (deleteSelectedContactsCommand = new RelayCommand(() => ExecuteDeleteSelectedContacts()));
}
}
public void ExecuteDeleteSelectedContacts()
{
var toDelete = this.Contacts.ToArray();
string msg = "Are you sure you want to delete the selected contacts?";
MessageBoxResult messageBoxResult = MessageBox.Show(msg, "Question!", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
foreach (var contact in toDelete)
{
if (contact.IsSelected)
{
this.Contacts.Remove(contact);
}
}
}
public void ExecuteDeleteContact(Contact contact)
{
string msg = "Are you sure you want to delete this contact?";
msg += "\n\n";
msg += "Name: " + contact.Name;
MessageBoxResult messageBoxResult = MessageBox.Show(msg, "Question!", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
this.Contacts.Remove(contact);
}
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
private Action methodToExecute;
private Func<bool> canExecuteEvaluator;
public RelayCommand(Action methodToExecute, Func<bool> canExecuteEvaluator)
{
this.methodToExecute = methodToExecute;
this.canExecuteEvaluator = canExecuteEvaluator;
}
public RelayCommand(Action methodToExecute)
: this(methodToExecute, null)
{
}
public bool CanExecute(object parameter)
{
if (this.canExecuteEvaluator == null)
{
return true;
}
else
{
bool result = this.canExecuteEvaluator.Invoke();
return result;
}
}
public void Execute(object parameter)
{
this.methodToExecute.Invoke();
}
}
using System;
using System.Windows.Input;
namespace Example.Helper
{
/// <summary>
/// Requires no arguments
/// </summary>
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Func<object, bool> canExecute;
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
/// <summary>
/// Expects arguments
/// </summary>
/// <typeparam name="T"></typeparam>
public class RelayCommand<T> : ICommand
{
private readonly Predicate<T> canExecute;
private readonly Action<T> execute;
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
if (execute == null)
throw new ArgumentNullException("execute");
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute((T)parameter);
}
public void Execute(object parameter)
{
execute((T)parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment