|
/* |
|
View USAGE: |
|
<UserControl xmlns="https://github.com/avaloniaui" |
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
|
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity" |
|
xmlns:ia="clr-namespace:Avalonia.Xaml.Interactions.Core;assembly=Avalonia.Xaml.Interactions" |
|
xmlns:ic="using:SuessLabs.Tool.Common.Behaviors" |
|
...> |
|
<Label Grid.Column="1" |
|
Content="{Binding MessageDisplayed}" |
|
FontStyle="Italic"> |
|
<i:Interaction.Behaviors> |
|
<ic:LabelClickEventTriggerBehavior KeyModifiers="Control"> |
|
<ia:InvokeCommandAction Command="{Binding CmdCopyText}" CommandParameter="{Binding MessageDisplayed}" /> |
|
</ic:LabelClickEventTriggerBehavior> |
|
</i:Interaction.Behaviors> |
|
</Label> |
|
|
|
<!-- Label with an Icon --> |
|
<Label Grid.Column="2" FontStyle="Italic"> |
|
<PathIcon Height="15" Data="{StaticResource document_copy_regular}" /> |
|
<i:Interaction.Behaviors> |
|
<ic:LabelClickEventTriggerBehavior KeyModifiers="Control"> |
|
<ia:InvokeCommandAction Command="{Binding CmdCopyText}" CommandParameter="{Binding MessageDisplayed}" /> |
|
</ic:LabelClickEventTriggerBehavior> |
|
</i:Interaction.Behaviors> |
|
</Label> |
|
*/ |
|
|
|
using Avalonia; |
|
using Avalonia.Controls; |
|
using Avalonia.Input; |
|
using Avalonia.Interactivity; |
|
using Avalonia.Xaml.Interactivity; |
|
|
|
namespace SuessLabs.Common.Behaviors; |
|
|
|
/// <summary> |
|
/// A behavior that listens for a <see cref="Button.ClickEvent"/> event on its source and executes its actions when that event is fired. |
|
/// </summary> |
|
public class LabelClickEventTriggerBehavior : Trigger<Label> |
|
{ |
|
/// <summary> |
|
/// Identifies the <seealso cref="KeyModifiers"/> avalonia property. |
|
/// </summary> |
|
public static readonly StyledProperty<KeyModifiers> KeyModifiersProperty = |
|
AvaloniaProperty.Register<LabelClickEventTriggerBehavior, KeyModifiers>(nameof(KeyModifiers)); |
|
|
|
/// <summary>Gets or sets a value indicating whether the pointer still inside of the control or not.</summary> |
|
private bool _isPointerInside; |
|
|
|
/// <summary> |
|
/// Gets or sets the required key modifiers to execute <see cref="Button.ClickEvent"/> event handler. This is a avalonia property. |
|
/// </summary> |
|
public KeyModifiers KeyModifiers |
|
{ |
|
get => GetValue(KeyModifiersProperty); |
|
set => SetValue(KeyModifiersProperty, value); |
|
} |
|
|
|
/// <inheritdoc /> |
|
protected override void OnAttachedToVisualTree() |
|
{ |
|
if (AssociatedObject is { }) |
|
{ |
|
AssociatedObject.Tapped += OnTapped; |
|
AssociatedObject.PointerEnter += OnPointerEnter; |
|
AssociatedObject.PointerLeave += OnPointerLeave; |
|
} |
|
} |
|
|
|
/// <inheritdoc /> |
|
protected override void OnDetachedFromVisualTree() |
|
{ |
|
if (AssociatedObject is { }) |
|
{ |
|
AssociatedObject.Tapped -= OnTapped; |
|
AssociatedObject.PointerEnter -= OnPointerEnter; |
|
AssociatedObject.PointerLeave -= OnPointerLeave; |
|
} |
|
} |
|
|
|
private void OnPointerEnter(object sender, PointerEventArgs e) |
|
{ |
|
_isPointerInside = true; |
|
} |
|
|
|
private void OnPointerLeave(object sender, PointerEventArgs e) |
|
{ |
|
_isPointerInside = false; |
|
} |
|
|
|
private void OnTapped(object? sender, RoutedEventArgs e) |
|
{ |
|
if (AssociatedObject is { } && _isPointerInside) |
|
{ |
|
Interaction.ExecuteActions(AssociatedObject, Actions, e); |
|
} |
|
} |
|
} |