testa
 
 
 
Internet Relay Command in WPF
cornice di chiusura
 
Relay Command in WPF
 

 public class RelayCommand<T> : ICommand
 {
  #region Private members
  /// <summary>
  /// Creates a new command that can always execute.
  /// </summary>
  private readonly Action<T> _execute;
  /// <summary>
  /// True if command is executing, false otherwise
  /// </summary>
  readonly Predicate<T> _canExecute = null;
  #endregion
  /// <summary>
  /// Initializes a new instance of <see cref="RelayCommand"/>
  /// </summary>
  /// <param name="execute">The execution logic.</param>
  /// <param name="canExecute">The execution status logic.</param>
  public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
  {
   if (execute == null)
   {
    throw new ArgumentNullException("execute");
   }
   _execute = execute;
   _canExecute = canExecute;
  }
  /// <summary>
  /// Raised when RaiseCanExecuteChanged is called.
  /// </summary>
  public event EventHandler CanExecuteChanged
  {
   add { CommandManager.RequerySuggested += value; }
   remove { CommandManager.RequerySuggested -= value; }
  }
  #region ICommand Members
  /// <summary>
  /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
  /// </summary>
  /// <param name="parameter">
  /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  /// </param>
  /// <returns>True if this command can be executed; otherwise, false.
  public bool CanExecute(object parameter)
  {
   return (_canExecute == null || _canExecute((T)parameter));
  }
  /// <summary>
  /// Executes the <see cref="RelayCommand"/> on the current command target.
  /// </summary>
  /// <param name="parameter">
  /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
  /// </param>
  public void Execute(object parameter)
  {
   _execute((T)parameter);
  }
  #endregion ICommand Members
 }
 public class RelayCommand : RelayCommand
 {
  public RelayCommand(Action execute)
  : base(execute, null)
  { }
  public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  : base(execute, canExecute)
  { }
 }

 

private ICommand _playMatch;
public ICommand PlayMatch
{
 get
 {
  if (_playMatch == null)
   _playMatch = new RelayCommand((p) => PlayMatch_Action());
 
  return _playMatch;
 }
}

 

<Button Command="{Binding PlayMatch, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />

 
 
 
I23 di Boccaletti Emanuele
 
I23 di Boccaletti Emanuele
41121 Modena (MO)
Tel. +39 347 1302420
emanuele@i23.eu
Skype: emanuele.boccaletti
 
 
Copyright © I23 di Boccaletti Emanuele - emanuele@i23.eu