Handle function insertion on ListBox row taps directly: add Tapped handler to function item Border in DataTemplate and remove global view-level tap handler. Ensures handler is tied to list rows, not the toggle button.

This commit is contained in:
Codex CLI 2025-08-28 04:12:33 -05:00
commit 781f7161e5
2 changed files with 6 additions and 18 deletions

View file

@ -156,6 +156,7 @@
<ListBox.ItemTemplate>
<DataTemplate x:DataType="m:FunctionDefinitionItem">
<Border Background="Transparent" Padding="8" MinHeight="36"
Tapped="OnFunctionsItemTapped"
AutomationProperties.Name="Insert function">
<Grid ColumnDefinitions="*,Auto">
<StackPanel>

View file

@ -21,7 +21,6 @@ public partial class MainView : UserControl
private MainViewModel? _vm;
private ListBox? _historyList;
private bool _functionsTapHandlerAttached;
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
{
@ -39,13 +38,7 @@ public partial class MainView : UserControl
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
}
// Handle taps anywhere in the functions flyout list items to insert function
if (!_functionsTapHandlerAttached)
{
this.AddHandler(InputElement.TappedEvent, OnFunctionsTapped,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
_functionsTapHandlerAttached = true;
}
// Functions list item taps are handled per-row via XAML (OnFunctionsItemTapped)
}
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
@ -59,11 +52,6 @@ public partial class MainView : UserControl
_historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped);
_historyList = null;
}
if (_functionsTapHandlerAttached)
{
this.RemoveHandler(InputElement.TappedEvent, OnFunctionsTapped);
_functionsTapHandlerAttached = false;
}
_vm = null;
}
@ -98,12 +86,11 @@ public partial class MainView : UserControl
}
}
private void OnFunctionsTapped(object? sender, TappedEventArgs e)
private void OnFunctionsItemTapped(object? sender, TappedEventArgs e)
{
// Find the ListBoxItem the tap originated from, and ensure it's a function item
var srcVisual = e.Source as IVisual;
var container = srcVisual?.FindAncestorOfType<ListBoxItem>();
if (container?.DataContext is not FunctionDefinitionItem item)
if (sender is not IControl control)
return;
if (control.DataContext is not FunctionDefinitionItem item)
return;
var vm = DataContext as MainViewModel;