DisabledCommand.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Windows.Input;
  6. namespace CommunityToolkit.Mvvm.Input.Internals;
  7. /// <summary>
  8. /// A reusable <see cref="ICommand"/> instance that is always disabled.
  9. /// </summary>
  10. internal sealed partial class DisabledCommand : ICommand
  11. {
  12. /// <inheritdoc/>
  13. public event EventHandler? CanExecuteChanged
  14. {
  15. add { }
  16. remove { }
  17. }
  18. /// <summary>
  19. /// Gets a shared, reusable <see cref="DisabledCommand"/> instance.
  20. /// </summary>
  21. /// <remarks>
  22. /// This instance can safely be used across multiple objects without having
  23. /// to worry about this static keeping others alive, as the event uses a
  24. /// custom accessor that just discards items (as the event is known to never
  25. /// be raised). As such, this instance will never act as root for other objects.
  26. /// </remarks>
  27. public static DisabledCommand Instance { get; } = new();
  28. /// <inheritdoc/>
  29. public bool CanExecute(object? parameter)
  30. {
  31. return false;
  32. }
  33. /// <inheritdoc/>
  34. public void Execute(object? parameter)
  35. {
  36. }
  37. }