diff --git a/samples/BehaviorsTestApplication/Properties/AssemblyInfo.cs b/samples/BehaviorsTestApplication/Properties/AssemblyInfo.cs index fdf0fc322..b248e909c 100644 --- a/samples/BehaviorsTestApplication/Properties/AssemblyInfo.cs +++ b/samples/BehaviorsTestApplication/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ -using System.Runtime.CompilerServices; -using Avalonia.Metadata; +using Avalonia.Metadata; [assembly: XmlnsDefinition("https://github.com/avaloniaui", "BehaviorsTestApplication")] [assembly: XmlnsDefinition("https://github.com/avaloniaui", "BehaviorsTestApplication.Controls")] diff --git a/samples/BehaviorsTestApplication/ViewModels/MainWindowViewModel.cs b/samples/BehaviorsTestApplication/ViewModels/MainWindowViewModel.cs index 4aa2fbff8..638b058a1 100644 --- a/samples/BehaviorsTestApplication/ViewModels/MainWindowViewModel.cs +++ b/samples/BehaviorsTestApplication/ViewModels/MainWindowViewModel.cs @@ -74,6 +74,8 @@ public MainWindowViewModel() ]; Values = Observable.Interval(TimeSpan.FromSeconds(1)).Select(_ => _value++); + + MyString = ""; } [Reactive] @@ -88,6 +90,9 @@ public MainWindowViewModel() [Reactive] public partial ObservableCollection? Items { get; set; } + + [Reactive] internal partial string MyString { get; set; } + public IObservable Values { get; } public ICommand InitializeCommand { get; set; } diff --git a/samples/BehaviorsTestApplication/Views/MainView.axaml b/samples/BehaviorsTestApplication/Views/MainView.axaml index 0201e86a3..789879489 100644 --- a/samples/BehaviorsTestApplication/Views/MainView.axaml +++ b/samples/BehaviorsTestApplication/Views/MainView.axaml @@ -22,6 +22,9 @@ + + + diff --git a/samples/BehaviorsTestApplication/Views/MainWindow.axaml.cs b/samples/BehaviorsTestApplication/Views/MainWindow.axaml.cs index 79b7efb48..b79412f75 100644 --- a/samples/BehaviorsTestApplication/Views/MainWindow.axaml.cs +++ b/samples/BehaviorsTestApplication/Views/MainWindow.axaml.cs @@ -1,7 +1,5 @@ using BehaviorsTestApplication.ViewModels; -using Avalonia; using Avalonia.Controls; -using Avalonia.Markup.Xaml; namespace BehaviorsTestApplication.Views; diff --git a/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml b/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml new file mode 100644 index 000000000..071f209c4 --- /dev/null +++ b/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml.cs b/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml.cs new file mode 100644 index 000000000..f2cbbeb7a --- /dev/null +++ b/samples/BehaviorsTestApplication/Views/Pages/DataTriggerBehaviorAdvancedView.axaml.cs @@ -0,0 +1,17 @@ +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace BehaviorsTestApplication.Views.Pages; + +public partial class DataTriggerBehaviorAdvancedView : UserControl +{ + public DataTriggerBehaviorAdvancedView() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } +} diff --git a/src/Avalonia.Xaml.Interactions.Custom/Button/ButtonClickEventTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions.Custom/Button/ButtonClickEventTriggerBehavior.cs index fb5e19e3d..380a8967a 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Button/ButtonClickEventTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Button/ButtonClickEventTriggerBehavior.cs @@ -50,6 +50,11 @@ protected override void OnDetachedFromVisualTree() } private void AssociatedObject_OnClick(object? sender, RoutedEventArgs e) + { + Execute(e); + } + + private void Execute(object? parameter) { if (!IsEnabled) { @@ -58,7 +63,7 @@ private void AssociatedObject_OnClick(object? sender, RoutedEventArgs e) if (AssociatedObject is not null && KeyModifiers == _savedKeyModifiers) { - Interaction.ExecuteActions(AssociatedObject, Actions, e); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/ActualThemeVariantChangedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/ActualThemeVariantChangedTrigger.cs index 8b39af952..35af3ab82 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/ActualThemeVariantChangedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/ActualThemeVariantChangedTrigger.cs @@ -9,12 +9,17 @@ public abstract class ActualThemeVariantChangedTrigger : StyledElementTrigger protected override void OnActualThemeVariantChangedEvent() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToLogicalTreeTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToLogicalTreeTrigger.cs index e813943ab..5b9582294 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToLogicalTreeTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToLogicalTreeTrigger.cs @@ -9,12 +9,17 @@ public abstract class AttachedToLogicalTreeTrigger : StyledElementTrigger protected override void OnAttachedToLogicalTree() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToVisualTreeTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToVisualTreeTrigger.cs index 45c8c3aad..0a8e17781 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToVisualTreeTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/AttachedToVisualTreeTrigger.cs @@ -9,12 +9,17 @@ public abstract class AttachedToVisualTreeTrigger : StyledElementTrigger { /// protected override void OnAttachedToVisualTree() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/BindingTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/BindingTriggerBehavior.cs index 40eef3e2f..6bfddb26e 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/BindingTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/BindingTriggerBehavior.cs @@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis; using Avalonia.Data; using Avalonia.Reactive; +using Avalonia.Threading; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; @@ -121,7 +122,10 @@ private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args) return; } - behavior.Execute(parameter: args); + Dispatcher.UIThread.Post(() => + { + behavior.Execute(parameter: args); + }); } private void Execute(object? parameter) diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/DataContextChangedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/DataContextChangedTrigger.cs index cbb29ac98..56b677105 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/DataContextChangedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/DataContextChangedTrigger.cs @@ -9,12 +9,17 @@ public abstract class DataContextChangedTrigger : StyledElementTrigger protected override void OnDataContextChangedEvent() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromLogicalTreeTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromLogicalTreeTrigger.cs index 585ef130f..9dfd93a79 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromLogicalTreeTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromLogicalTreeTrigger.cs @@ -9,12 +9,17 @@ public abstract class DetachedFromLogicalTreeTrigger : StyledElementTrigger protected override void OnDetachedFromLogicalTree() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromVisualTreeTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromVisualTreeTrigger.cs index 29321a763..67d74d53d 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromVisualTreeTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/DetachedFromVisualTreeTrigger.cs @@ -9,12 +9,17 @@ public abstract class DetachedFromVisualTreeTrigger : StyledElementTrigger protected override void OnDetachedFromVisualTree() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/InitializedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/InitializedTrigger.cs index 4618f50f1..2e7704036 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/InitializedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/InitializedTrigger.cs @@ -9,12 +9,17 @@ public abstract class InitializedTrigger : StyledElementTrigger { /// protected override void OnInitializedEvent() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/LoadedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/LoadedTrigger.cs index 504f15c43..20b35a610 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/LoadedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/LoadedTrigger.cs @@ -10,12 +10,17 @@ public abstract class LoadedTrigger : StyledElementTrigger { /// protected override void OnLoaded() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/ResourcesChangedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/ResourcesChangedTrigger.cs index 7094d8a4b..f7748bc9f 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/ResourcesChangedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/ResourcesChangedTrigger.cs @@ -9,12 +9,17 @@ public abstract class ResourcesChangedTrigger : StyledElementTrigger protected override void OnResourcesChangedEvent() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/RoutedEventTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/RoutedEventTriggerBehavior.cs index 635f603ba..1c41e95bd 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/RoutedEventTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/RoutedEventTriggerBehavior.cs @@ -1,6 +1,5 @@ using Avalonia.Controls; using Avalonia.Interactivity; -using Avalonia.Reactive; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; @@ -61,19 +60,28 @@ public Interactive? SourceInteractive set => SetValue(SourceInteractiveProperty, value); } - static RoutedEventTriggerBehavior() + /// + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { - RoutedEventProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + base.OnPropertyChanged(change); + + if (change.Property == RoutedEventProperty) + { + OnValueChanged(change); + } - RoutingStrategiesProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + if (change.Property == RoutingStrategiesProperty) + { + OnValueChanged(change); + } - SourceInteractiveProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + if (change.Property == SourceInteractiveProperty) + { + OnValueChanged(change); + } } - private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args) + private void OnValueChanged(AvaloniaPropertyChangedEventArgs args) { if (args.Sender is not RoutedEventTriggerBehavior behavior || behavior.AssociatedObject is null) { @@ -127,6 +135,11 @@ private void RemoveHandler() } private void Handler(object? sender, RoutedEventArgs e) + { + Execute(e); + } + + private void Execute(object? parameter) { if (!IsEnabled) { @@ -136,7 +149,7 @@ private void Handler(object? sender, RoutedEventArgs e) var interactive = ComputeResolvedSourceInteractive(); if (interactive is not null) { - Interaction.ExecuteActions(interactive, Actions, e); + Interaction.ExecuteActions(interactive, Actions, parameter); } } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/UnloadedTrigger.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/UnloadedTrigger.cs index 85ccd9e61..d4d294fb3 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/UnloadedTrigger.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/UnloadedTrigger.cs @@ -10,12 +10,17 @@ public abstract class UnloadedTrigger : StyledElementTrigger { /// protected override void OnUnloaded() + { + Execute(parameter: null); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(AssociatedObject, Actions, parameter: null); + Interaction.ExecuteActions(AssociatedObject, Actions, parameter); } } diff --git a/src/Avalonia.Xaml.Interactions.Custom/Core/ValueChangedTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions.Custom/Core/ValueChangedTriggerBehavior.cs index 93f173b7a..d2674cff6 100644 --- a/src/Avalonia.Xaml.Interactions.Custom/Core/ValueChangedTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions.Custom/Core/ValueChangedTriggerBehavior.cs @@ -1,4 +1,4 @@ -using Avalonia.Reactive; +using Avalonia.Threading; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; @@ -8,12 +8,6 @@ namespace Avalonia.Xaml.Interactions.Custom; /// public class ValueChangedTriggerBehavior : StyledElementTrigger { - static ValueChangedTriggerBehavior() - { - BindingProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); - } - /// /// Identifies the avalonia property. /// @@ -29,14 +23,29 @@ public object? Binding set => SetValue(BindingProperty, value); } - private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args) + + /// + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == BindingProperty) + { + OnValueChanged(change); + } + } + + private void OnValueChanged(AvaloniaPropertyChangedEventArgs args) { if (args.Sender is not ValueChangedTriggerBehavior behavior) { return; } - behavior.Execute(args); + Dispatcher.UIThread.Post(() => + { + behavior.Execute(args); + }); } private void Execute(object? parameter) diff --git a/src/Avalonia.Xaml.Interactions.DragAndDrop/TypedDragBehavior.cs b/src/Avalonia.Xaml.Interactions.DragAndDrop/TypedDragBehavior.cs index 63f9967f5..5933fd1c3 100644 --- a/src/Avalonia.Xaml.Interactions.DragAndDrop/TypedDragBehavior.cs +++ b/src/Avalonia.Xaml.Interactions.DragAndDrop/TypedDragBehavior.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Input; diff --git a/src/Avalonia.Xaml.Interactions/Core/CallMethodAction.cs b/src/Avalonia.Xaml.Interactions/Core/CallMethodAction.cs index 37c4c9ff7..c0868a39d 100644 --- a/src/Avalonia.Xaml.Interactions/Core/CallMethodAction.cs +++ b/src/Avalonia.Xaml.Interactions/Core/CallMethodAction.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Threading.Tasks; using Avalonia.Controls; -using Avalonia.Reactive; namespace Avalonia.Xaml.Interactions.Core; @@ -51,34 +50,41 @@ public object? TargetObject set => SetValue(TargetObjectProperty, value); } - static CallMethodAction() + /// + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { - MethodNameProperty.Changed.Subscribe( - new AnonymousObserver>(MethodNameChanged)); + base.OnPropertyChanged(change); + + if (change.Property == MethodNameProperty) + { + MethodNameChanged(change); + } - TargetObjectProperty.Changed.Subscribe( - new AnonymousObserver>(TargetObjectChanged)); + if (change.Property == TargetObjectProperty) + { + TargetObjectChanged(change); + } } - private static void MethodNameChanged(AvaloniaPropertyChangedEventArgs e) + private void MethodNameChanged(AvaloniaPropertyChangedEventArgs e) { if (e.Sender is not CallMethodAction callMethodAction) { return; } - + callMethodAction.UpdateMethodDescriptors(); } [RequiresUnreferencedCode("This functionality is not compatible with trimming.")] - private static void TargetObjectChanged(AvaloniaPropertyChangedEventArgs e) + private void TargetObjectChanged(AvaloniaPropertyChangedEventArgs e) { if (e.Sender is not CallMethodAction callMethodAction) { return; } - var newValue = e.NewValue.GetValueOrDefault(); + var newValue = e.GetNewValue(); if (newValue is not null) { var newType = newValue.GetType(); diff --git a/src/Avalonia.Xaml.Interactions/Core/DataTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions/Core/DataTriggerBehavior.cs index 6ea67f4cb..566b5f3dc 100644 --- a/src/Avalonia.Xaml.Interactions/Core/DataTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions/Core/DataTriggerBehavior.cs @@ -1,6 +1,5 @@ -using System; -using System.Diagnostics.CodeAnalysis; -using Avalonia.Reactive; +using System.Diagnostics.CodeAnalysis; +using Avalonia.Threading; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Core; @@ -56,16 +55,25 @@ public object? Value set => SetValue(ValueProperty, value); } - static DataTriggerBehavior() + /// + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { - BindingProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + base.OnPropertyChanged(change); + + if (change.Property == BindingProperty) + { + OnValueChanged(change); + } - ComparisonConditionProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + if (change.Property == ComparisonConditionProperty) + { + OnValueChanged(change); + } - ValueProperty.Changed.Subscribe( - new AnonymousObserver>(OnValueChanged)); + if (change.Property == ValueProperty) + { + OnValueChanged(change); + } } /// @@ -76,14 +84,17 @@ protected override void OnInitializedEvent() Execute(parameter: null); } - private static void OnValueChanged(AvaloniaPropertyChangedEventArgs args) + private void OnValueChanged(AvaloniaPropertyChangedEventArgs args) { if (args.Sender is not DataTriggerBehavior behavior) { return; } - behavior.Execute(parameter: args); + Dispatcher.UIThread.Post(() => + { + behavior.Execute(parameter: args); + }); } private void Execute(object? parameter) diff --git a/src/Avalonia.Xaml.Interactions/Core/EventTriggerBehavior.cs b/src/Avalonia.Xaml.Interactions/Core/EventTriggerBehavior.cs index cee15f962..0a42e886c 100644 --- a/src/Avalonia.Xaml.Interactions/Core/EventTriggerBehavior.cs +++ b/src/Avalonia.Xaml.Interactions/Core/EventTriggerBehavior.cs @@ -239,13 +239,18 @@ private void UnregisterEvent(string? eventName) /// The sender object. /// The event args. protected virtual void AttachedToVisualTree(object? sender, object eventArgs) + { + Execute(eventArgs); + } + + private void Execute(object? parameter) { if (!IsEnabled) { return; } - Interaction.ExecuteActions(_resolvedSource, Actions, eventArgs); + Interaction.ExecuteActions(_resolvedSource, Actions, parameter); } private static bool IsElementLoaded(Control element) => element.Parent is not null; diff --git a/src/Avalonia.Xaml.Interactivity/StyledElementAction.cs b/src/Avalonia.Xaml.Interactivity/StyledElementAction.cs index 33feec854..e5af7cdd2 100644 --- a/src/Avalonia.Xaml.Interactivity/StyledElementAction.cs +++ b/src/Avalonia.Xaml.Interactivity/StyledElementAction.cs @@ -11,7 +11,8 @@ public abstract class StyledElementAction : StyledElement, IAction /// Identifies the avalonia property. /// public static readonly StyledProperty IsEnabledProperty = - AvaloniaProperty.Register(nameof(IsEnabled), defaultValue: true); + AvaloniaProperty.Register(nameof(IsEnabled), + defaultValue: true); /// /// Gets or sets a value indicating whether this instance is enabled. @@ -32,6 +33,11 @@ public bool IsEnabled /// Returns the result of the action. public abstract object? Execute(object? sender, object? parameter); + internal void Initialize() + { + InitializeIfNeeded(); + } + internal void AttachActionToLogicalTree(StyledElement parent) { // Required for $parent binding in XAML diff --git a/src/Avalonia.Xaml.Interactivity/StyledElementBehavior.cs b/src/Avalonia.Xaml.Interactivity/StyledElementBehavior.cs index 3e79bd047..e6ff91fe8 100644 --- a/src/Avalonia.Xaml.Interactivity/StyledElementBehavior.cs +++ b/src/Avalonia.Xaml.Interactivity/StyledElementBehavior.cs @@ -133,7 +133,12 @@ void IBehaviorEventsHandler.DetachedFromLogicalTreeEventHandler() void IBehaviorEventsHandler.UnloadedEventHandler() => OnUnloaded(); - void IBehaviorEventsHandler.InitializedEventHandler() => OnInitializedEvent(); + void IBehaviorEventsHandler.InitializedEventHandler() + { + Initialize(); + + OnInitializedEvent(); + } void IBehaviorEventsHandler.DataContextChangedEventHandler() => OnDataContextChangedEvent(); @@ -241,6 +246,11 @@ protected virtual void OnActualThemeVariantChangedEvent() { } + internal virtual void Initialize() + { + InitializeIfNeeded(); + } + internal virtual void AttachBehaviorToLogicalTree() { StyledElement? parent = null; diff --git a/src/Avalonia.Xaml.Interactivity/StyledElementBehaviorOfT.cs b/src/Avalonia.Xaml.Interactivity/StyledElementBehaviorOfT.cs index bbd83009e..2f86e8660 100644 --- a/src/Avalonia.Xaml.Interactivity/StyledElementBehaviorOfT.cs +++ b/src/Avalonia.Xaml.Interactivity/StyledElementBehaviorOfT.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; namespace Avalonia.Xaml.Interactivity; diff --git a/src/Avalonia.Xaml.Interactivity/StyledElementTrigger.cs b/src/Avalonia.Xaml.Interactivity/StyledElementTrigger.cs index c778f5d41..8583f4011 100644 --- a/src/Avalonia.Xaml.Interactivity/StyledElementTrigger.cs +++ b/src/Avalonia.Xaml.Interactivity/StyledElementTrigger.cs @@ -22,6 +22,19 @@ public abstract class StyledElementTrigger : StyledElementBehavior, ITrigger [Content] public ActionCollection Actions => _actions ??= []; + internal override void Initialize() + { + base.Initialize(); + + foreach (var action in Actions) + { + if (action is StyledElementAction styledElementAction) + { + styledElementAction.Initialize(); + } + } + } + internal override void AttachBehaviorToLogicalTree() { base.AttachBehaviorToLogicalTree(); diff --git a/src/Avalonia.Xaml.Interactivity/StyledElementTriggerOfT.cs b/src/Avalonia.Xaml.Interactivity/StyledElementTriggerOfT.cs index 329d0d895..d44a2e9d6 100644 --- a/src/Avalonia.Xaml.Interactivity/StyledElementTriggerOfT.cs +++ b/src/Avalonia.Xaml.Interactivity/StyledElementTriggerOfT.cs @@ -1,6 +1,4 @@ using System; -using System.Diagnostics.CodeAnalysis; -using Avalonia.Metadata; namespace Avalonia.Xaml.Interactivity;