RoutedEventTriggerBehavior.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Avalonia.Controls;
  2. using Avalonia.Interactivity;
  3. using Avalonia.Reactive;
  4. using Avalonia.Xaml.Interactivity;
  5. namespace Avalonia.Xaml.Interactions.Custom;
  6. /// <summary>
  7. /// A behavior that listens for a <see cref="RoutedEvent"/> event on its source and executes its actions when that event is fired.
  8. /// </summary>
  9. public class RoutedEventTriggerBehavior : StyledElementTrigger<Interactive>
  10. {
  11. /// <summary>
  12. /// Identifies the <seealso cref="RoutedEvent"/> avalonia property.
  13. /// </summary>
  14. public static readonly StyledProperty<RoutedEvent?> RoutedEventProperty =
  15. AvaloniaProperty.Register<RoutedEventTriggerBehavior, RoutedEvent?>(nameof(RoutedEvent));
  16. /// <summary>
  17. /// Identifies the <seealso cref="RoutingStrategies"/> avalonia property.
  18. /// </summary>
  19. public static readonly StyledProperty<RoutingStrategies> RoutingStrategiesProperty =
  20. AvaloniaProperty.Register<RoutedEventTriggerBehavior, RoutingStrategies>(nameof(RoutingStrategies),
  21. RoutingStrategies.Direct | RoutingStrategies.Bubble);
  22. /// <summary>
  23. /// Identifies the <seealso cref="SourceInteractive"/> avalonia property.
  24. /// </summary>
  25. public static readonly StyledProperty<Interactive?> SourceInteractiveProperty =
  26. AvaloniaProperty.Register<RoutedEventTriggerBehavior, Interactive?>(nameof(SourceInteractive));
  27. private bool _isInitialized;
  28. private bool _isAttached;
  29. /// <summary>
  30. /// Gets or sets routing event to listen for. This is a avalonia property.
  31. /// </summary>
  32. public RoutedEvent? RoutedEvent
  33. {
  34. get => GetValue(RoutedEventProperty);
  35. set => SetValue(RoutedEventProperty, value);
  36. }
  37. /// <summary>
  38. /// Gets or sets the routing event <see cref="RoutingStrategies"/>. This is a avalonia property.
  39. /// </summary>
  40. public RoutingStrategies RoutingStrategies
  41. {
  42. get => GetValue(RoutingStrategiesProperty);
  43. set => SetValue(RoutingStrategiesProperty, value);
  44. }
  45. /// <summary>
  46. /// Gets or sets the source object from which this behavior listens for events.
  47. /// If <seealso cref="SourceInteractive"/> is not set, the source will default to <seealso cref="IBehavior.AssociatedObject"/>. This is a avalonia property.
  48. /// </summary>
  49. [ResolveByName]
  50. public Interactive? SourceInteractive
  51. {
  52. get => GetValue(SourceInteractiveProperty);
  53. set => SetValue(SourceInteractiveProperty, value);
  54. }
  55. static RoutedEventTriggerBehavior()
  56. {
  57. RoutedEventProperty.Changed.Subscribe(
  58. new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutedEvent?>>(OnValueChanged));
  59. RoutingStrategiesProperty.Changed.Subscribe(
  60. new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutingStrategies>>(OnValueChanged));
  61. SourceInteractiveProperty.Changed.Subscribe(
  62. new AnonymousObserver<AvaloniaPropertyChangedEventArgs<Interactive?>>(OnValueChanged));
  63. }
  64. private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args)
  65. {
  66. if (args.Sender is not RoutedEventTriggerBehavior behavior || behavior.AssociatedObject is null)
  67. {
  68. return;
  69. }
  70. if (behavior._isInitialized && behavior._isAttached)
  71. {
  72. behavior.RemoveHandler();
  73. behavior.AddHandler();
  74. }
  75. }
  76. /// <inheritdoc />
  77. protected override void OnAttachedToVisualTree()
  78. {
  79. _isAttached = true;
  80. AddHandler();
  81. }
  82. /// <inheritdoc />
  83. protected override void OnDetachedFromVisualTree()
  84. {
  85. _isAttached = false;
  86. RemoveHandler();
  87. }
  88. private void AddHandler()
  89. {
  90. var interactive = ComputeResolvedSourceInteractive();
  91. if (interactive is not null && RoutedEvent is not null)
  92. {
  93. interactive.AddHandler(RoutedEvent, Handler, RoutingStrategies);
  94. _isInitialized = true;
  95. }
  96. }
  97. private void RemoveHandler()
  98. {
  99. var interactive = ComputeResolvedSourceInteractive();
  100. if (interactive is not null && RoutedEvent is not null && _isInitialized)
  101. {
  102. interactive.RemoveHandler(RoutedEvent, Handler);
  103. _isInitialized = false;
  104. }
  105. }
  106. private Interactive? ComputeResolvedSourceInteractive()
  107. {
  108. return GetValue(SourceInteractiveProperty) is not null ? SourceInteractive : AssociatedObject;
  109. }
  110. private void Handler(object? sender, RoutedEventArgs e)
  111. {
  112. if (!IsEnabled)
  113. {
  114. return;
  115. }
  116. var interactive = ComputeResolvedSourceInteractive();
  117. if (interactive is not null)
  118. {
  119. Interaction.ExecuteActions(interactive, Actions, e);
  120. }
  121. }
  122. }