Skip to content

Commit

Permalink
Merge pull request #47 from wieslawsoltes/FixTriggers
Browse files Browse the repository at this point in the history
Fix triggers
  • Loading branch information
wieslawsoltes authored Mar 4, 2025
2 parents 5049e68 + eb401a0 commit ee7faea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public MainWindowViewModel()
IsLoading = true;
Progress = 30;

DataContextChangedCommand = ReactiveCommand.Create(DataContextChanged);

InitializeCommand = ReactiveCommand.Create(Initialize);

MoveLeftCommand = ReactiveCommand.Create(() => Position -= 5.0);
Expand Down Expand Up @@ -115,6 +117,8 @@ public MainWindowViewModel()

public IObservable<int> Values { get; }

public ICommand DataContextChangedCommand { get; set; }

public ICommand InitializeCommand { get; set; }

public ICommand MoveLeftCommand { get; set; }
Expand All @@ -133,6 +137,11 @@ public MainWindowViewModel()

public ICommand GetClipboardTextCommand { get; set; }

private void DataContextChanged()
{
Console.WriteLine("DataContextChanged");
}

private void Initialize()
{
Console.WriteLine("InitializeCommand");
Expand Down
7 changes: 7 additions & 0 deletions samples/BehaviorsTestApplication/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,23 @@
Title="XamlBehaviors Test Application" Width="1000" Height="700"
x:DataType="vm:MainWindowViewModel">
<Interaction.Behaviors>

<!-- TopLevel Logical tree special case -->
<!--
<EventTriggerBehavior EventName="Opened">
<InvokeCommandAction Command="{Binding InitializeCommand, Mode=OneTime}" />
</EventTriggerBehavior>
-->

<!-- TopLevel attached to visual tree special case -->
<RoutedEventTriggerBehavior RoutedEvent="{x:Static Control.LoadedEvent}">
<InvokeCommandAction Command="{Binding InitializeCommand, Mode=OneTime}" />
</RoutedEventTriggerBehavior>

<DataContextChangedTrigger>
<InvokeCommandAction Command="{Binding DataContextChangedCommand}" />
</DataContextChangedTrigger>

</Interaction.Behaviors>
<views:MainView />
</Window>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Avalonia.Threading;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;
Expand All @@ -10,7 +11,10 @@ public class ActualThemeVariantChangedTrigger : StyledElementTrigger<StyledEleme
/// <inheritdoc />
protected override void OnActualThemeVariantChangedEvent()
{
Execute(parameter: null);
Dispatcher.UIThread.Post(() =>
{
Execute(parameter: null);
});
}

private void Execute(object? parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Avalonia.Threading;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;
Expand All @@ -10,7 +11,10 @@ public class DataContextChangedTrigger : StyledElementTrigger<StyledElement>
/// <inheritdoc />
protected override void OnDataContextChangedEvent()
{
Execute(parameter: null);
Dispatcher.UIThread.Post(() =>
{
Execute(parameter: null);
});
}

private void Execute(object? parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Avalonia.Threading;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;
Expand All @@ -10,7 +11,10 @@ public class ResourcesChangedTrigger : StyledElementTrigger<StyledElement>
/// <inheritdoc />
protected override void OnResourcesChangedEvent()
{
Execute(parameter: null);
Dispatcher.UIThread.Post(() =>
{
Execute(parameter: null);
});
}

private void Execute(object? parameter)
Expand Down

0 comments on commit ee7faea

Please sign in to comment.