using Avalonia.Reactive; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// A behavior that performs actions when the bound data produces new value. /// public class ValueChangedTriggerBehavior : StyledElementTrigger { static ValueChangedTriggerBehavior() { BindingProperty.Changed.Subscribe( new AnonymousObserver>(OnValueChanged)); } /// /// Identifies the avalonia property. /// public static readonly StyledProperty BindingProperty = AvaloniaProperty.Register(nameof(Binding)); /// /// Gets or sets the bound object that the will listen to. This is a avalonia property. /// public object? Binding { get => GetValue(BindingProperty); set => SetValue(BindingProperty, value); } private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args) { if (args.Sender is not ValueChangedTriggerBehavior behavior || behavior.AssociatedObject is null) { return; } if (!behavior.IsEnabled) { return; } var binding = behavior.Binding; if (binding is not null) { Interaction.ExecuteActions(behavior.AssociatedObject, behavior.Actions, args); } } }