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,32 +71,23 @@
|
||||||
<Grid RowDefinitions="*,Auto">
|
<Grid RowDefinitions="*,Auto">
|
||||||
<!-- History -->
|
<!-- History -->
|
||||||
<Grid Grid.Row="0">
|
<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">
|
AutomationProperties.Name="History list">
|
||||||
<ListBox.ItemTemplate>
|
<ListBox.ItemTemplate>
|
||||||
<DataTemplate x:DataType="m:HistoryItem">
|
<DataTemplate x:DataType="m:HistoryItem">
|
||||||
<!-- Make the entire row a single Button so the whole item is clickable -->
|
<!-- Entire row UI; ListBox-level Tapped handler triggers copy -->
|
||||||
<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"
|
<Grid x:Name="HistoryRow" ColumnDefinitions="Auto,*,Auto" Margin="4,2"
|
||||||
DataContext="{Binding $parent[ListBoxItem].DataContext}"
|
DataContext="{Binding $parent[ListBoxItem].DataContext}"
|
||||||
x:DataType="m:HistoryItem">
|
x:DataType="m:HistoryItem">
|
||||||
<TextBlock Grid.Column="0" FontFamily="{StaticResource MDI}" Text="{x:Static m:IconFont.ArrowRightDropCircle}"
|
<TextBlock Grid.Column="0" FontFamily="{StaticResource MDI}" Text="{x:Static m:IconFont.ArrowRightDropCircle}"
|
||||||
FontSize="{DynamicResource IconSizeM}"
|
FontSize="{DynamicResource IconSizeM}" VerticalAlignment="Center" Margin="0,0,8,0" />
|
||||||
VerticalAlignment="Center" Margin="0,0,8,0" />
|
|
||||||
<StackPanel Grid.Column="1">
|
<StackPanel Grid.Column="1">
|
||||||
<TextBlock Text="{Binding Input}" MaxLines="3" />
|
<TextBlock Text="{Binding Input}" MaxLines="3" />
|
||||||
<TextBlock Text="{Binding Output}" FontWeight="Bold" MaxLines="2" />
|
<TextBlock Text="{Binding Output}" FontWeight="Bold" MaxLines="2" />
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
|
|
||||||
<!-- Context menu for right-click / long-press (Android) -->
|
<!-- Context menu for right-click / long-press (touch) -->
|
||||||
<Grid.ContextMenu>
|
<Grid.ContextMenu>
|
||||||
<ContextMenu>
|
<ContextMenu>
|
||||||
<MenuItem x:DataType="vm:MainViewModel"
|
<MenuItem x:DataType="vm:MainViewModel"
|
||||||
|
@ -117,7 +108,6 @@
|
||||||
</ContextMenu>
|
</ContextMenu>
|
||||||
</Grid.ContextMenu>
|
</Grid.ContextMenu>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Button>
|
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</ListBox.ItemTemplate>
|
</ListBox.ItemTemplate>
|
||||||
</ListBox>
|
</ListBox>
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Input;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Avalonia.VisualTree;
|
||||||
using AdvancedCalculator.ViewModels;
|
using AdvancedCalculator.ViewModels;
|
||||||
|
using AdvancedCalculator.Models;
|
||||||
|
|
||||||
namespace AdvancedCalculator.Views;
|
namespace AdvancedCalculator.Views;
|
||||||
|
|
||||||
|
@ -15,6 +19,7 @@ public partial class MainView : UserControl
|
||||||
}
|
}
|
||||||
|
|
||||||
private MainViewModel? _vm;
|
private MainViewModel? _vm;
|
||||||
|
private ListBox? _historyList;
|
||||||
|
|
||||||
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||||
{
|
{
|
||||||
|
@ -23,6 +28,14 @@ public partial class MainView : UserControl
|
||||||
{
|
{
|
||||||
_vm.CopyRequested += OnCopyRequested;
|
_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)
|
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||||
|
@ -31,6 +44,11 @@ public partial class MainView : UserControl
|
||||||
{
|
{
|
||||||
_vm.CopyRequested -= OnCopyRequested;
|
_vm.CopyRequested -= OnCopyRequested;
|
||||||
}
|
}
|
||||||
|
if (_historyList is not null)
|
||||||
|
{
|
||||||
|
_historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped);
|
||||||
|
_historyList = null;
|
||||||
|
}
|
||||||
_vm = null;
|
_vm = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,4 +65,21 @@ public partial class MainView : UserControl
|
||||||
// Ignore clipboard errors; e.g., browser permission or missing gesture context
|
// 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