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
|
@ -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