/ src / settings-ui / Settings.UI.Library / ViewModels / Commands / ButtonClickCommand.cs
ButtonClickCommand.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Windows.Input;
 7  
 8  namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands
 9  {
10      public class ButtonClickCommand : ICommand
11      {
12          private readonly Action _execute;
13  
14          public ButtonClickCommand(Action execute)
15          {
16              _execute = execute;
17          }
18  
19          // Occurs when changes occur that affect whether or not the command should execute.
20          public event EventHandler CanExecuteChanged;
21  
22          // Defines the method that determines whether the command can execute in its current state.
23          public bool CanExecute(object parameter)
24          {
25              return true;
26          }
27  
28          // Defines the method to be called when the command is invoked.
29          public void Execute(object parameter)
30          {
31              _execute();
32          }
33  
34          public void OnCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
35      }
36  }