private void AssociatedObject_PreviewKeyDown(object sender, KeyEventArgs e) { if (!AutoCompletePopup.IsOpen) { return; } bool close = false; switch (e.Key) { case Key.Escape: close = true; break; case Key.Return: InsertText(); close = true; break; case Key.Up: AutoCompleteBox.SelectedIndex--; e.Handled = true; break; case Key.Down: AutoCompleteBox.SelectedIndex++; e.Handled = true; break; case Key.Back: if (FilterText.Length > 0) { FilterText = FilterText.Substring(0, FilterText.Length - 1); } else { close = true; } break; } int itemCount = FilteredItems.Count(); if (AutoCompleteBox.SelectedIndex < 0) { AutoCompleteBox.SelectedIndex = itemCount - 1; } if (AutoCompleteBox.SelectedIndex >= itemCount) { AutoCompleteBox.SelectedIndex = 0; } if (close) { e.Handled = true; CloseAutoCompleteBox(); } }