Use ListBoxItem taps to insert functions: remove inner Button from functions list template, keep item DataContext as FunctionDefinitionItem, and handle tap in code-behind by executing InsertFunctionCommand with the tapped item. Mirror history list clipboard pattern. Name flyout root for clarity.
This commit is contained in:
parent
ad8e178484
commit
8a5a50fcaa
2 changed files with 33 additions and 6 deletions
|
@ -135,7 +135,7 @@
|
|||
AutomationProperties.Name="Toggle functions panel">
|
||||
<Button.Flyout>
|
||||
<Flyout IsOpen="{Binding IsFunctionsPanelOpen, Mode=TwoWay}" Placement="BottomEdgeAlignedLeft">
|
||||
<Border Padding="8" MinWidth="360" MaxHeight="420">
|
||||
<Border x:Name="FunctionsPanelRoot" Padding="8" MinWidth="360" MaxHeight="420">
|
||||
<ScrollViewer>
|
||||
<StackPanel>
|
||||
<ItemsControl ItemsSource="{x:Static m:FunctionDefinitionItem.DefinedFunctionGroups}">
|
||||
|
@ -155,10 +155,7 @@
|
|||
<ListBox ItemsSource="{Binding Functions}" BorderThickness="0" SelectedIndex="-1">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate x:DataType="m:FunctionDefinitionItem">
|
||||
<Button
|
||||
Command="{Binding $parent[ListBox].DataContext.InsertFunctionCommand}"
|
||||
CommandParameter="{Binding}"
|
||||
Background="Transparent" BorderThickness="0" Padding="8" MinHeight="36"
|
||||
<Border Background="Transparent" Padding="8" MinHeight="36"
|
||||
AutomationProperties.Name="Insert function">
|
||||
<Grid ColumnDefinitions="*,Auto">
|
||||
<StackPanel>
|
||||
|
@ -166,7 +163,7 @@
|
|||
<TextBlock Text="{Binding FunctionDescription}" Opacity="0.8"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Button>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
|
|
@ -21,6 +21,7 @@ public partial class MainView : UserControl
|
|||
|
||||
private MainViewModel? _vm;
|
||||
private ListBox? _historyList;
|
||||
private bool _functionsTapHandlerAttached;
|
||||
|
||||
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
|
@ -37,6 +38,14 @@ public partial class MainView : UserControl
|
|||
_historyList.AddHandler(InputElement.TappedEvent, OnHistoryTapped,
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
|
@ -50,6 +59,11 @@ public partial class MainView : UserControl
|
|||
_historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped);
|
||||
_historyList = null;
|
||||
}
|
||||
if (_functionsTapHandlerAttached)
|
||||
{
|
||||
this.RemoveHandler(InputElement.TappedEvent, OnFunctionsTapped);
|
||||
_functionsTapHandlerAttached = false;
|
||||
}
|
||||
_vm = null;
|
||||
}
|
||||
|
||||
|
@ -83,4 +97,20 @@ public partial class MainView : UserControl
|
|||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFunctionsTapped(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)
|
||||
return;
|
||||
|
||||
var vm = DataContext as MainViewModel;
|
||||
if (vm?.InsertFunctionCommand.CanExecute(item) == true)
|
||||
{
|
||||
vm.InsertFunctionCommand.Execute(item);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue