mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 22:23:29 -07:00
Fix Focus bug by proactively moving focus before the ListView is updated
This commit is contained in:
parent
233339d289
commit
71420e88c9
1 changed files with 19 additions and 0 deletions
|
@ -57,9 +57,28 @@ void Memory::MemoryListItemClick(_In_ Object ^ sender, _In_ ItemClickEventArgs ^
|
|||
|
||||
void Memory::OnClearMenuItemClicked(_In_ Object ^ sender, _In_ RoutedEventArgs ^ e)
|
||||
{
|
||||
// We are proactively hiding the context menu so that XAML focus goes back to a ListViewItem.
|
||||
// If we let the context menu hide on its own then it would lose focus and get hidden after the
|
||||
// coresponding memory item is removed. The XAML Focus Manager will then try to set focus to the
|
||||
// item that no longer exists and will end up setting focus to the root of the ListView
|
||||
// because the item that XAML wants to give focus to does not exist.
|
||||
MemoryContextMenu->Hide();
|
||||
|
||||
auto memoryItem = GetMemoryItemForCurrentFlyout();
|
||||
if (memoryItem != nullptr)
|
||||
{
|
||||
// Move focus to a neighboaring item before removing memoryItem. Ideally the same index in
|
||||
// the list will be selected after the remove operation, but if we remove the last item then
|
||||
// we want the new last item to have focus.
|
||||
auto items = MemoryListView->Items->GetView();
|
||||
auto lastItem = items->GetAt(items->Size - 1);
|
||||
auto direction = FocusNavigationDirection::Down;
|
||||
if (lastItem == memoryItem)
|
||||
{
|
||||
direction = FocusNavigationDirection::Up;
|
||||
}
|
||||
FocusManager::TryMoveFocus(direction);
|
||||
|
||||
memoryItem->Clear();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue