fix(ui): context menu operates on right-clicked item and gates new-task on list selection
- Add PointerPressed handlers on list/task item templates that set SelectedList/SelectedTask on right-click before the ContextFlyout opens - Add CanAddTask guard and NotifyCanExecuteChangedFor on CurrentListId so Add Task menu item is disabled when no list is selected Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -24,7 +24,7 @@ public partial class TaskListViewModel : ViewModelBase
|
|||||||
public ObservableCollection<TaskItemViewModel> Tasks { get; } = new();
|
public ObservableCollection<TaskItemViewModel> Tasks { get; } = new();
|
||||||
|
|
||||||
[ObservableProperty] private TaskItemViewModel? _selectedTask;
|
[ObservableProperty] private TaskItemViewModel? _selectedTask;
|
||||||
[ObservableProperty] private string? _currentListId;
|
[ObservableProperty, NotifyCanExecuteChangedFor(nameof(AddTaskCommand))] private string? _currentListId;
|
||||||
|
|
||||||
public event Action<TaskItemViewModel?>? SelectedTaskChanged;
|
public event Action<TaskItemViewModel?>? SelectedTaskChanged;
|
||||||
|
|
||||||
@@ -69,7 +69,9 @@ public partial class TaskListViewModel : ViewModelBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
private bool CanAddTask() => CurrentListId is not null;
|
||||||
|
|
||||||
|
[RelayCommand(CanExecute = nameof(CanAddTask))]
|
||||||
private async Task AddTask()
|
private async Task AddTask()
|
||||||
{
|
{
|
||||||
if (CurrentListId is null) return;
|
if (CurrentListId is null) return;
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
Margin="4">
|
Margin="4">
|
||||||
<ListBox.ItemTemplate>
|
<ListBox.ItemTemplate>
|
||||||
<DataTemplate x:DataType="vm:ListItemViewModel">
|
<DataTemplate x:DataType="vm:ListItemViewModel">
|
||||||
<StackPanel Margin="4,2" DoubleTapped="OnListItemDoubleTapped">
|
<StackPanel Margin="4,2" DoubleTapped="OnListItemDoubleTapped" PointerPressed="OnListItemPointerPressed">
|
||||||
<StackPanel.ContextFlyout>
|
<StackPanel.ContextFlyout>
|
||||||
<MenuFlyout>
|
<MenuFlyout>
|
||||||
<MenuItem Header="Edit"
|
<MenuItem Header="Edit"
|
||||||
|
|||||||
@@ -27,4 +27,14 @@ public partial class MainWindow : Window
|
|||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnListItemPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.GetCurrentPoint(null).Properties.PointerUpdateKind == PointerUpdateKind.RightButtonPressed
|
||||||
|
&& sender is Control c && c.DataContext is ListItemViewModel item
|
||||||
|
&& DataContext is MainWindowViewModel vm)
|
||||||
|
{
|
||||||
|
vm.SelectedList = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
Margin="4">
|
Margin="4">
|
||||||
<ListBox.ItemTemplate>
|
<ListBox.ItemTemplate>
|
||||||
<DataTemplate x:DataType="vm:TaskItemViewModel">
|
<DataTemplate x:DataType="vm:TaskItemViewModel">
|
||||||
<Grid ColumnDefinitions="*,Auto,Auto" Margin="4,2" DoubleTapped="OnTaskItemDoubleTapped">
|
<Grid ColumnDefinitions="*,Auto,Auto" Margin="4,2" DoubleTapped="OnTaskItemDoubleTapped" PointerPressed="OnTaskItemPointerPressed">
|
||||||
<Grid.ContextFlyout>
|
<Grid.ContextFlyout>
|
||||||
<MenuFlyout>
|
<MenuFlyout>
|
||||||
<MenuItem Header="Edit"
|
<MenuItem Header="Edit"
|
||||||
|
|||||||
@@ -20,4 +20,14 @@ public partial class TaskListView : UserControl
|
|||||||
e.Handled = true;
|
e.Handled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnTaskItemPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.GetCurrentPoint(null).Properties.PointerUpdateKind == PointerUpdateKind.RightButtonPressed
|
||||||
|
&& sender is Control c && c.DataContext is TaskItemViewModel item
|
||||||
|
&& DataContext is TaskListViewModel vm)
|
||||||
|
{
|
||||||
|
vm.SelectedTask = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user