Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions samples/BehaviorsTestApplication/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public MainWindowViewModel()
];

Values = Observable.Interval(TimeSpan.FromSeconds(1)).Select(_ => _value++);

MyString = "";
}

[Reactive]
Expand All @@ -88,6 +90,9 @@ public MainWindowViewModel()
[Reactive]
public partial ObservableCollection<ItemViewModel>? Items { get; set; }


[Reactive] internal partial string MyString { get; set; }

public IObservable<int> Values { get; }

public ICommand InitializeCommand { get; set; }
Expand Down
3 changes: 3 additions & 0 deletions samples/BehaviorsTestApplication/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<TabItem Header="DataTriggerBehavior">
<pages:DataTriggerBehaviorView />
</TabItem>
<TabItem Header="DataTriggerBehavior Advanced">
<pages:DataTriggerBehaviorAdvancedView />
</TabItem>
<TabItem Header="BindingTriggerBehavior">
<pages:BindingTriggerBehaviorView />
</TabItem>
Expand Down
2 changes: 0 additions & 2 deletions samples/BehaviorsTestApplication/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BehaviorsTestApplication.ViewModels;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace BehaviorsTestApplication.Views;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<UserControl x:Class="BehaviorsTestApplication.Views.Pages.DataTriggerBehaviorAdvancedView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:BehaviorsTestApplication.ViewModels"
x:DataType="vm:MainWindowViewModel"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450">
<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>
<Grid RowDefinitions="Auto,*">
<TextBox Grid.Row="0" Text="{Binding MyString}" />
<Border Grid.Row="1" Background="Black" CornerRadius="5">
<Interaction.Behaviors>
<DataTriggerBehavior Binding="{Binding MyString}" ComparisonCondition="Equal" Value="">
<ChangePropertyAction PropertyName="IsVisible" Value="False" />
</DataTriggerBehavior>
<DataTriggerBehavior Binding="{Binding MyString}" ComparisonCondition="NotEqual" Value="">
<ChangePropertyAction PropertyName="IsVisible" Value="True" />
</DataTriggerBehavior>
</Interaction.Behaviors>
<StackPanel>
<TextBlock FontSize="25" Foreground="White">
<Interaction.Behaviors>
<DataTriggerBehavior Binding="{Binding MyString}" ComparisonCondition="NotEqual" Value="">
<ChangePropertyAction PropertyName="Text" Value="{Binding MyString}" />
</DataTriggerBehavior>
</Interaction.Behaviors>
</TextBlock>
</StackPanel>
</Border>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class ActualThemeVariantChangedTrigger : StyledElementTrigger<St
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class AttachedToLogicalTreeTrigger : StyledElementTrigger<Styled
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class AttachedToVisualTreeTrigger : StyledElementTrigger<Visual>
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class DataContextChangedTrigger : StyledElementTrigger<StyledEle
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class DetachedFromLogicalTreeTrigger : StyledElementTrigger<Styl
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class DetachedFromVisualTreeTrigger : StyledElementTrigger<Visua
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class InitializedTrigger : StyledElementTrigger<StyledElement>
{
/// <inheritdoc />
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);
}
}
7 changes: 6 additions & 1 deletion src/Avalonia.Xaml.Interactions.Custom/Core/LoadedTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ public abstract class LoadedTrigger : StyledElementTrigger<Control>
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public abstract class ResourcesChangedTrigger : StyledElementTrigger<StyledEleme
{
/// <inheritdoc />
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Reactive;
using Avalonia.Xaml.Interactivity;

namespace Avalonia.Xaml.Interactions.Custom;
Expand Down Expand Up @@ -61,19 +60,28 @@ public Interactive? SourceInteractive
set => SetValue(SourceInteractiveProperty, value);
}

static RoutedEventTriggerBehavior()
/// <inheritdoc />
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
RoutedEventProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutedEvent?>>(OnValueChanged));
base.OnPropertyChanged(change);

if (change.Property == RoutedEventProperty)
{
OnValueChanged(change);
}

RoutingStrategiesProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<RoutingStrategies>>(OnValueChanged));
if (change.Property == RoutingStrategiesProperty)
{
OnValueChanged(change);
}

SourceInteractiveProperty.Changed.Subscribe(
new AnonymousObserver<AvaloniaPropertyChangedEventArgs<Interactive?>>(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)
{
Expand Down Expand Up @@ -127,6 +135,11 @@ private void RemoveHandler()
}

private void Handler(object? sender, RoutedEventArgs e)
{
Execute(e);
}

private void Execute(object? parameter)
{
if (!IsEnabled)
{
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ public abstract class UnloadedTrigger : StyledElementTrigger<Control>
{
/// <inheritdoc />
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);
}
}
Loading
Loading