private void OnActivated(object sender, EventArgs e) { var windows = SystemWindow.FilterToplevelWindows(new Predicate <SystemWindow>(IsAppWindow)); Array.Sort(windows, (wnd1, wnd2) => wnd1.Title.CompareTo(wnd2.Title)); // // If an item was already selected in the list, try to find it in the new list and // select it. // var selectedWindow = WindowList.SelectedItem as SystemWindow; m_Windows.Clear(); m_Windows.AddRange(windows); WindowList.Items.Refresh(); CollectionViewSource.GetDefaultView(WindowList.ItemsSource).Refresh(); // // If the previously selected window is not found in the new list, simply select // the first element. // int selectIndex = 0; if (selectedWindow != null) { for (int i = 0; i < WindowList.Items.Count; ++i) { SystemWindow wnd = WindowList.Items[i] as SystemWindow; if (wnd.HWnd == selectedWindow.HWnd) { selectIndex = i; break; } } } if (!WindowList.Items.IsEmpty) { WindowList.SelectedIndex = selectIndex; WindowList.UpdateLayout(); (WindowList.ItemContainerGenerator.ContainerFromIndex(selectIndex) as ListViewItem).Focus(); } FuzzySearchTitle.Focus(); }