From c9f536c63537fbdc93866d526797e4adb4cf6ae8 Mon Sep 17 00:00:00 2001 From: Alekhya Date: Wed, 2 Sep 2020 13:39:57 -0700 Subject: [PATCH] fix for argument out of range exception (#6269) --- .../launcher/PowerLauncher/MainWindow.xaml.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index f7af050031..e8afdeb7ce 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -15,6 +15,7 @@ using PowerLauncher.Helper; using PowerLauncher.ViewModel; using Wox.Infrastructure.UserSettings; using KeyEventArgs = System.Windows.Input.KeyEventArgs; +using Log = Wox.Infrastructure.Logger.Log; using Screen = System.Windows.Forms.Screen; namespace PowerLauncher @@ -302,7 +303,20 @@ namespace PowerLauncher _viewModel.Results.SelectedItem = (ResultViewModel)listview.SelectedItem; if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) { - listview.ScrollIntoView(e.AddedItems[0]); + try + { + listview.ScrollIntoView(e.AddedItems[0]); + } + catch (ArgumentOutOfRangeException ex) + { + // Due to virtualization being enabled for the listview, the layout system updates elements in a deferred manner using an algorithm that balances performance and concurrency. + // Hence, there can be a situation where the element index that we want to scroll into view is out of range for it's parent control. + // To mitigate this we use the UpdateLayout function, which forces layout update to ensure that the parent element contains the latest properties. + // However, it has a performance impact and is therefore not called each time. + Log.Exception("MainWindow", "The parent element layout is not updated yet", ex, "SuggestionsList_SelectionChanged"); + listview.UpdateLayout(); + listview.ScrollIntoView(e.AddedItems[0]); + } } // To populate the AutoCompleteTextBox as soon as the selection is changed or set.