Skip to content

Instantly share code, notes, and snippets.

@DamianSuess
Last active May 24, 2024 21:10
Show Gist options
  • Save DamianSuess/d4af84772720db73781dd9f35eb65fcd to your computer and use it in GitHub Desktop.
Save DamianSuess/d4af84772720db73781dd9f35eb65fcd to your computer and use it in GitHub Desktop.
Avalonia - DoubleTapped Behavior
<UserControl ...>
<!-- Sample 1 - ListBox Data Template - Dobule-Tapped -->
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate x:DataType="models:HardwareEvent" x:Key="BleAdvertisement">
<Panel Background="Transparent">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="DoubleTapped">
<!-- Must set the parent, otherwise it will look for the 'Command' in the template's DataType -->
<ia:InvokeCommandAction Command="{Binding $parent.((vm:BluetoothViewModel)DataContext).CmdCopyText}"
CommandParameter="{Binding $parent.((vm:BluetoothViewModel)DataContext).EventItemText}" />
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
<Grid ColumnDefinitions="Auto,Auto,Auto">
<Label Content="{Binding CreatedAt, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"
Grid.Column="0"
FontSize="10" />
<Label Content="{Binding MacAddress}"
Grid.Column="1"
FontSize="10" />
<Label Content="{Binding Rssi}"
Grid.Column="2"
FontSize="10" />
</Grid>
</Panel>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<!-- Sample 2 - Glyph Label - Single-Tap -->
<Grid RowDefinitions="Auto,Auto">
<Label FontStyle="Italic">
<PathIcon Height="15" Data="{StaticResource document_copy_regular}" />
<i:Interaction.Behaviors>
<ic:CommandOnTappedBehavior Command="{Binding CmdCopyText}" CommandParameter="{Binding ResponseCode}" />
</i:Interaction.Behaviors>
</Label>
<ListBox Grid.Row="1"
ItemTemplate="{StaticResource BleAdvertisement}"
FontFamily="Consolas"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible"
SelectionMode="Single" />
</Grid>
</UserControl>
// NOTE: The notifications uses Prism.Avalonia, and Clipboard is Avalonia v0.10
public DelegateCommand<string> CmdCopyText => new(async (text) =>
{
if (string.IsNullOrEmpty(text) || Application.Current is null || Application.Current.Clipboard is null)
return;
await Application.Current.Clipboard.SetTextAsync(text);
_notifications.Show("Clipboard", "Contents copied to clipboard");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment