spiegelp / MaterialDesignExtensions

Material Design Extensions is based on Material Design in XAML Toolkit to provide additional controls and features for WPF apps
https://spiegelp.github.io/MaterialDesignExtensions/
MIT License
754 stars 122 forks source link

TextBoxSuggestions should return focus to the TextBox on selection #73

Closed beechj closed 4 years ago

beechj commented 4 years ago

I am using the TextBoxSuggestions control inside a DataGridColumn template.

When the user selects an item from the control the keyboard focus is not returned to the TextBox and so it makes it very difficult commit the edit from the keyboard because the control that had focus is removed from the visual tree and so none of the controls have focus. You have to click into the grid again to apply the change.

You can fix this behaviour quite simply by changing the SelectSuggestionItemCommandHandler like so:

private void SelectSuggestionItemCommandHandler(object sender, ExecutedRoutedEventArgs args)
{
    if (TextBox == null) return;
    TextBox.Text = args.Parameter as string ?? "";
    TextBox.Focus();
    TextBox.CaretIndex = TextBox.Text.Length;
}

This enables the user to apply a change by pressing enter twice, once to select the item from the list and once to commit the edit to the row.

It will also allow the user to continue tabbing through the view as normal for anyone who is not using the control inside a DataGrid.

spiegelp commented 4 years ago

@beechj I added a KeepFocusOnSelection property to TextBoxSuggestions. Setting it to true will enable your desired behavior.

beechj commented 4 years ago

Thank you @spiegelp