Skip to content

Commit 47e1c90

Browse files
committed
mark the event as handled if an item was selected by pressing TAB so that the base.OnPreviewKeyDown does not move focus to the next element
1 parent 431c3ee commit 47e1c90

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,36 @@ public override void OnApplyTemplate()
145145

146146
protected override void OnPreviewKeyDown(KeyEventArgs e)
147147
{
148-
base.OnPreviewKeyDown(e);
149148
if (_autoSuggestBoxList is null) return;
150149
switch (e.Key)
151150
{
152151
case Key.Down:
153152
IncrementSelection();
153+
e.Handled = true;
154154
break;
155155
case Key.Up:
156156
DecrementSelection();
157+
e.Handled = true;
157158
break;
158159
case Key.Enter:
159160
CommitValueSelection();
161+
e.Handled = true;
160162
break;
161163
case Key.Escape:
162164
CloseAutoSuggestionPopUp();
165+
e.Handled = true;
163166
break;
164167
case Key.Tab:
165-
CommitValueSelection();
168+
bool wasItemSelected = CommitValueSelection();
169+
if (wasItemSelected)
170+
{
171+
e.Handled = true;
172+
}
166173
break;
167174
default:
168175
return;
169176
}
177+
base.OnPreviewKeyDown(e);
170178
}
171179

172180
protected override void OnLostFocus(RoutedEventArgs e)
@@ -227,14 +235,14 @@ private void CloseAutoSuggestionPopUp()
227235
IsSuggestionOpen = false;
228236
}
229237

230-
private void CommitValueSelection()
238+
private bool CommitValueSelection()
231239
=> CommitValueSelection(_autoSuggestBoxList?.SelectedValue);
232240

233-
private void CommitValueSelection(object? selectedValue)
241+
private bool CommitValueSelection(object? selectedValue)
234242
{
235243
if (IsSuggestionOpen == false)
236244
{
237-
return;
245+
return false;
238246
}
239247

240248
string oldValue = Text;
@@ -249,6 +257,7 @@ private void CommitValueSelection(object? selectedValue)
249257
RoutedEvent = SuggestionChosenEvent
250258
};
251259
RaiseEvent(args);
260+
return true;
252261
}
253262

254263
private void DecrementSelection()

0 commit comments

Comments
 (0)