AddClassAction.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Avalonia.Controls;
  2. using Avalonia.Xaml.Interactivity;
  3. namespace Avalonia.Xaml.Interactions.Custom;
  4. /// <summary>
  5. /// Adds a specified <see cref="AddClassAction.ClassName"/> to the <see cref="StyledElement.Classes"/> collection when invoked.
  6. /// </summary>
  7. public class AddClassAction : Avalonia.Xaml.Interactivity.Action
  8. {
  9. /// <summary>
  10. /// Identifies the <seealso cref="ClassName"/> avalonia property.
  11. /// </summary>
  12. public static readonly StyledProperty<string> ClassNameProperty =
  13. AvaloniaProperty.Register<AddClassAction, string>(nameof(ClassName));
  14. /// <summary>
  15. /// Identifies the <seealso cref="StyledElement"/> avalonia property.
  16. /// </summary>
  17. public static readonly StyledProperty<StyledElement?> StyledElementProperty =
  18. AvaloniaProperty.Register<AddClassAction, StyledElement?>(nameof(StyledElement));
  19. /// <summary>
  20. /// Identifies the <seealso cref="RemoveIfExists"/> avalonia property.
  21. /// </summary>
  22. public static readonly StyledProperty<bool> RemoveIfExistsProperty =
  23. AvaloniaProperty.Register<AddClassAction, bool>(nameof(RemoveIfExists));
  24. /// <summary>
  25. /// Gets or sets the class name that should be added. This is a avalonia property.
  26. /// </summary>
  27. public string ClassName
  28. {
  29. get => GetValue(ClassNameProperty);
  30. set => SetValue(ClassNameProperty, value);
  31. }
  32. /// <summary>
  33. /// Gets or sets the target styled element that class name that should be added to. This is a avalonia property.
  34. /// </summary>
  35. [ResolveByName]
  36. public StyledElement? StyledElement
  37. {
  38. get => GetValue(StyledElementProperty);
  39. set => SetValue(StyledElementProperty, value);
  40. }
  41. /// <summary>
  42. /// Gets or sets the flag indicated whether to remove the class if already exists before adding. This is a avalonia property.
  43. /// </summary>
  44. public bool RemoveIfExists
  45. {
  46. get => GetValue(RemoveIfExistsProperty);
  47. set => SetValue(RemoveIfExistsProperty, value);
  48. }
  49. /// <summary>
  50. /// Executes the action.
  51. /// </summary>
  52. /// <param name="sender">The <see cref="object"/> that is passed to the action by the behavior. Generally this is <seealso cref="IBehavior.AssociatedObject"/> or a target object.</param>
  53. /// <param name="parameter">The value of this parameter is determined by the caller.</param>
  54. /// <returns>True if the class is successfully added; else false.</returns>
  55. public override object Execute(object? sender, object? parameter)
  56. {
  57. if (!IsEnabled)
  58. {
  59. return false;
  60. }
  61. var target = GetValue(StyledElementProperty) is not null ? StyledElement : sender as StyledElement;
  62. if (target is null || string.IsNullOrEmpty(ClassName))
  63. {
  64. return false;
  65. }
  66. if (RemoveIfExists && target.Classes.Contains(ClassName))
  67. {
  68. target.Classes.Remove(ClassName);
  69. }
  70. target.Classes.Add(ClassName);
  71. return true;
  72. }
  73. }