Behavior.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using Avalonia.Controls;
  5. namespace Avalonia.Xaml.Interactivity;
  6. /// <summary>
  7. /// A base class for behaviors, implementing the basic plumbing of <see cref="IBehavior"/>.
  8. /// </summary>
  9. public abstract class Behavior : AvaloniaObject, IBehavior, IBehaviorEventsHandler
  10. {
  11. /// <summary>
  12. /// Identifies the <seealso cref="IsEnabled"/> avalonia property.
  13. /// </summary>
  14. public static readonly StyledProperty<bool> IsEnabledProperty =
  15. AvaloniaProperty.Register<Behavior, bool>(nameof(IsEnabled), defaultValue: true);
  16. /// <summary>
  17. /// Gets the <see cref="AvaloniaObject"/> to which the behavior is attached.
  18. /// </summary>
  19. public AvaloniaObject? AssociatedObject { get; private set; }
  20. /// <summary>
  21. /// Gets or sets a value indicating whether this instance is enabled.
  22. /// </summary>
  23. /// <value><c>true</c> if this instance is enabled; otherwise, <c>false</c>.</value>
  24. public bool IsEnabled
  25. {
  26. get => GetValue(IsEnabledProperty);
  27. set => SetValue(IsEnabledProperty, value);
  28. }
  29. /// <summary>
  30. /// Attaches the behavior to the specified <see cref="AvaloniaObject"/>.
  31. /// </summary>
  32. /// <param name="associatedObject">The <see cref="AvaloniaObject"/> to which to attach.</param>
  33. /// <exception cref="ArgumentNullException"><paramref name="associatedObject"/> is null.</exception>
  34. public void Attach(AvaloniaObject? associatedObject)
  35. {
  36. if (Equals(associatedObject, AssociatedObject))
  37. {
  38. return;
  39. }
  40. if (AssociatedObject is not null)
  41. {
  42. throw new InvalidOperationException(string.Format(
  43. CultureInfo.CurrentCulture,
  44. "An instance of a behavior cannot be attached to more than one object at a time."));
  45. }
  46. Debug.Assert(associatedObject is not null, "Cannot attach the behavior to a null object.");
  47. AssociatedObject = associatedObject ?? throw new ArgumentNullException(nameof(associatedObject));
  48. OnAttached();
  49. }
  50. /// <summary>
  51. /// Detaches the behaviors from the <see cref="AssociatedObject"/>.
  52. /// </summary>
  53. public void Detach()
  54. {
  55. OnDetaching();
  56. AssociatedObject = null;
  57. }
  58. /// <summary>
  59. /// Called after the behavior is attached to the <see cref="AssociatedObject"/>.
  60. /// </summary>
  61. /// <remarks>
  62. /// Override this to hook up functionality to the <see cref="AssociatedObject"/>
  63. /// </remarks>
  64. protected virtual void OnAttached()
  65. {
  66. }
  67. /// <summary>
  68. /// Called when the behavior is being detached from its <see cref="AssociatedObject"/>.
  69. /// </summary>
  70. /// <remarks>
  71. /// Override this to unhook functionality from the <see cref="AssociatedObject"/>
  72. /// </remarks>
  73. protected virtual void OnDetaching()
  74. {
  75. }
  76. void IBehaviorEventsHandler.AttachedToVisualTreeEventHandler() => OnAttachedToVisualTree();
  77. void IBehaviorEventsHandler.DetachedFromVisualTreeEventHandler() => OnDetachedFromVisualTree();
  78. void IBehaviorEventsHandler.AttachedToLogicalTreeEventHandler() => OnAttachedToLogicalTree();
  79. void IBehaviorEventsHandler.DetachedFromLogicalTreeEventHandler() => OnDetachedFromLogicalTree();
  80. void IBehaviorEventsHandler.LoadedEventHandler() => OnLoaded();
  81. void IBehaviorEventsHandler.UnloadedEventHandler() => OnUnloaded();
  82. /// <summary>
  83. /// Called after the <see cref="AssociatedObject"/> is attached to the visual tree.
  84. /// </summary>
  85. /// <remarks>
  86. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  87. /// </remarks>
  88. protected virtual void OnAttachedToVisualTree()
  89. {
  90. }
  91. /// <summary>
  92. /// Called when the <see cref="AssociatedObject"/> is being detached from the visual tree.
  93. /// </summary>
  94. /// <remarks>
  95. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  96. /// </remarks>
  97. protected virtual void OnDetachedFromVisualTree()
  98. {
  99. }
  100. /// <summary>
  101. /// Called after the <see cref="AssociatedObject"/> is attached to the logical tree.
  102. /// </summary>
  103. /// <remarks>
  104. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  105. /// </remarks>
  106. protected virtual void OnAttachedToLogicalTree()
  107. {
  108. }
  109. /// <summary>
  110. /// Called when the <see cref="AssociatedObject"/> is being detached from the logical tree.
  111. /// </summary>
  112. /// <remarks>
  113. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  114. /// </remarks>
  115. protected virtual void OnDetachedFromLogicalTree()
  116. {
  117. }
  118. /// <summary>
  119. /// Called after the <see cref="AssociatedObject"/> is loaded.
  120. /// </summary>
  121. /// <remarks>
  122. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  123. /// </remarks>
  124. protected virtual void OnLoaded()
  125. {
  126. }
  127. /// <summary>
  128. /// Called when the <see cref="AssociatedObject"/> is unloaded.
  129. /// </summary>
  130. /// <remarks>
  131. /// Invoked only when the <see cref="AssociatedObject"/> is of type <see cref="Control"/>.
  132. /// </remarks>
  133. protected virtual void OnUnloaded()
  134. {
  135. }
  136. }