| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.WixBA |
| 4 | { |
| 5 | using System; |
| 6 | using System.Diagnostics; |
| 7 | using System.Windows.Input; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Base class that implements ICommand interface via delegates. |
| 11 | /// </summary> |
| 12 | public class RelayCommand : ICommand |
| 13 | { |
| 14 | private readonly Action<object> execute; |
| 15 | private readonly Predicate<object> canExecute; |
| 16 | |
| 17 | public RelayCommand(Action<object> execute) |
| 18 | : this(execute, null) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | public RelayCommand(Action<object> execute, Predicate<object> canExecute) |
| 23 | { |
| 24 | this.execute = execute; |
| 25 | this.canExecute = canExecute; |
| 26 | } |
| 27 | |
| 28 | public event EventHandler CanExecuteChanged |
| 29 | { |
| 30 | add { CommandManager.RequerySuggested += value; } |
| 31 | remove { CommandManager.RequerySuggested -= value; } |
| 32 | } |
| 33 | |
| 34 | [DebuggerStepThrough] |
| 35 | public bool CanExecute(object parameter) |
| 36 | { |
| 37 | return this.canExecute == null ? true : this.canExecute(parameter); |
| 38 | } |
| 39 | |
| 40 | public void Execute(object parameter) |
| 41 | { |
| 42 | this.execute(parameter); |
| 43 | } |
| 44 | } |
| 45 | } |