History: handle Tapped at ListBox level to copy output for entire row; switch template back to Grid with ContextMenu; stretch item content for full-row target.
This commit is contained in:
parent
5702fd6c98
commit
cb589569a1
2 changed files with 68 additions and 43 deletions
|
@ -71,53 +71,43 @@
|
|||
<Grid RowDefinitions="*,Auto">
|
||||
<!-- History -->
|
||||
<Grid Grid.Row="0">
|
||||
<ListBox ItemsSource="{Binding History}" SelectedIndex="{Binding SelectedHistoryIndex}"
|
||||
<ListBox x:Name="HistoryList" ItemsSource="{Binding History}" SelectedIndex="{Binding SelectedHistoryIndex}"
|
||||
HorizontalContentAlignment="Stretch"
|
||||
AutomationProperties.Name="History list">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate x:DataType="m:HistoryItem">
|
||||
<!-- Make the entire row a single Button so the whole item is clickable -->
|
||||
<Button x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Command="{Binding CopyHistoryOutputCommand}"
|
||||
CommandParameter="{Binding $parent[ListBoxItem].DataContext}"
|
||||
Background="Transparent"
|
||||
BorderThickness="0"
|
||||
Padding="0"
|
||||
MinHeight="40"
|
||||
AutomationProperties.Name="Copy history item">
|
||||
<Grid x:Name="HistoryRow" ColumnDefinitions="Auto,*,Auto" Margin="4,2"
|
||||
DataContext="{Binding $parent[ListBoxItem].DataContext}"
|
||||
x:DataType="m:HistoryItem">
|
||||
<TextBlock Grid.Column="0" FontFamily="{StaticResource MDI}" Text="{x:Static m:IconFont.ArrowRightDropCircle}"
|
||||
FontSize="{DynamicResource IconSizeM}"
|
||||
VerticalAlignment="Center" Margin="0,0,8,0" />
|
||||
<StackPanel Grid.Column="1">
|
||||
<TextBlock Text="{Binding Input}" MaxLines="3" />
|
||||
<TextBlock Text="{Binding Output}" FontWeight="Bold" MaxLines="2" />
|
||||
</StackPanel>
|
||||
<!-- Entire row UI; ListBox-level Tapped handler triggers copy -->
|
||||
<Grid x:Name="HistoryRow" ColumnDefinitions="Auto,*,Auto" Margin="4,2"
|
||||
DataContext="{Binding $parent[ListBoxItem].DataContext}"
|
||||
x:DataType="m:HistoryItem">
|
||||
<TextBlock Grid.Column="0" FontFamily="{StaticResource MDI}" Text="{x:Static m:IconFont.ArrowRightDropCircle}"
|
||||
FontSize="{DynamicResource IconSizeM}" VerticalAlignment="Center" Margin="0,0,8,0" />
|
||||
<StackPanel Grid.Column="1">
|
||||
<TextBlock Text="{Binding Input}" MaxLines="3" />
|
||||
<TextBlock Text="{Binding Output}" FontWeight="Bold" MaxLines="2" />
|
||||
</StackPanel>
|
||||
|
||||
<!-- Context menu for right-click / long-press (Android) -->
|
||||
<Grid.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Input"
|
||||
Command="{Binding CopyHistoryInputCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Output"
|
||||
Command="{Binding CopyHistoryOutputCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Input = Output"
|
||||
Command="{Binding CopyHistoryBothCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
</ContextMenu>
|
||||
</Grid.ContextMenu>
|
||||
</Grid>
|
||||
</Button>
|
||||
<!-- Context menu for right-click / long-press (touch) -->
|
||||
<Grid.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Input"
|
||||
Command="{Binding CopyHistoryInputCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Output"
|
||||
Command="{Binding CopyHistoryOutputCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
<MenuItem x:DataType="vm:MainViewModel"
|
||||
DataContext="{Binding #Root.DataContext}"
|
||||
Header="Copy Input = Output"
|
||||
Command="{Binding CopyHistoryBothCommand}"
|
||||
CommandParameter="{Binding #HistoryRow.DataContext}" />
|
||||
</ContextMenu>
|
||||
</Grid.ContextMenu>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.VisualTree;
|
||||
using AdvancedCalculator.ViewModels;
|
||||
using AdvancedCalculator.Models;
|
||||
|
||||
namespace AdvancedCalculator.Views;
|
||||
|
||||
|
@ -15,6 +19,7 @@ public partial class MainView : UserControl
|
|||
}
|
||||
|
||||
private MainViewModel? _vm;
|
||||
private ListBox? _historyList;
|
||||
|
||||
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
|
@ -23,6 +28,14 @@ public partial class MainView : UserControl
|
|||
{
|
||||
_vm.CopyRequested += OnCopyRequested;
|
||||
}
|
||||
|
||||
_historyList = this.FindControl<ListBox>("HistoryList");
|
||||
if (_historyList is not null)
|
||||
{
|
||||
// Handle taps anywhere in a history row to copy output text
|
||||
_historyList.AddHandler(InputElement.TappedEvent, OnHistoryTapped,
|
||||
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
|
@ -31,6 +44,11 @@ public partial class MainView : UserControl
|
|||
{
|
||||
_vm.CopyRequested -= OnCopyRequested;
|
||||
}
|
||||
if (_historyList is not null)
|
||||
{
|
||||
_historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped);
|
||||
_historyList = null;
|
||||
}
|
||||
_vm = null;
|
||||
}
|
||||
|
||||
|
@ -47,4 +65,21 @@ public partial class MainView : UserControl
|
|||
// Ignore clipboard errors; e.g., browser permission or missing gesture context
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHistoryTapped(object? sender, TappedEventArgs e)
|
||||
{
|
||||
// Find the ListBoxItem the tap originated from
|
||||
var source = e.Source as IVisual;
|
||||
var container = source?.FindAncestorOfType<ListBoxItem>();
|
||||
if (container?.DataContext is not HistoryItem item)
|
||||
return;
|
||||
|
||||
var vm = DataContext as MainViewModel;
|
||||
if (vm?.CopyHistoryOutputCommand.CanExecute(item) == true)
|
||||
{
|
||||
vm.CopyHistoryOutputCommand.Execute(item);
|
||||
// Mark handled to avoid unintended selection change on tap
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue