using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace Tango.Editors { /// /// RelayCommand is a very easy-to-use implementation of ICommand. You can use a RelayCommand to expose viewmodel functionality as a command, and /// supply the condition that determines the command's availability. A control in the view bound to a command can execute an available command will /// update its enabled state in response to the availability of the command. /// public sealed class RelayCommand : ICommand { #region fields readonly Predicate canExecute; readonly Action execute; private Action saveProject; private RelayCommand syncHardwareConfigurationCommand; #endregion fields #region constructors public RelayCommand(Action execute) : this(execute, null) { } public RelayCommand(Action execute, Predicate canExecute) { this.execute = execute; this.canExecute = canExecute; } public RelayCommand(Action execute) : this((x) => execute()) { } public RelayCommand(Action execute, Predicate canExecute) : this((x) => execute(), canExecute) { } public RelayCommand(Action saveProject) { this.saveProject = saveProject; } public RelayCommand(RelayCommand syncHardwareConfigurationCommand) { this.syncHardwareConfigurationCommand = syncHardwareConfigurationCommand; } #endregion constructors #region methods public void RaiseCanExecuteChanged() { if (this.CanExecuteChanged != null) { this.CanExecuteChanged(this, EventArgs.Empty); } } #endregion methods #region ICommand events public event EventHandler CanExecuteChanged; #endregion ICommand events #region ICommand methods public bool CanExecute(object parameter) { return this.canExecute != null ? this.canExecute(parameter) : true; } public void Execute(object parameter) { if (this.execute != null) { this.execute(parameter); } } #endregion ICommand methods } public sealed class RelayCommand : ICommand { #region fields readonly Predicate canExecute; readonly Action execute; #endregion fields #region constructors public RelayCommand(Action execute) : this(execute, null) { } public RelayCommand(Action execute, Predicate canExecute) { this.execute = execute; this.canExecute = canExecute; } public RelayCommand(Action execute) : this((x) => execute()) { } #endregion constructors #region methods public void RaiseCanExecuteChanged() { if (this.CanExecuteChanged != null) { this.CanExecuteChanged(this, EventArgs.Empty); } } #endregion methods #region ICommand events public event EventHandler CanExecuteChanged; #endregion ICommand events #region ICommand methods public bool CanExecute(object parameter) { return this.canExecute != null ? this.canExecute(parameter) : true; } public void Execute(object parameter) { if (this.execute != null) { this.execute((T)parameter); } } #endregion ICommand methods } }