WPF-AutoComplete-TextBox icon indicating copy to clipboard operation
WPF-AutoComplete-TextBox copied to clipboard

What is the event of user's pressing enter on a candidate?

Open HubKing opened this issue 4 years ago • 3 comments

Think of Google search. You press down arrow to focus a candidate, press enter, and the search begins. I want to achieve the same thing. There seems no documentation but only two sample projects. In the demo, selecting a state and pressing enter changes the state name on the right, and I think this is what catches the event.

SelectedItem="{Binding State, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

But that is XAML binding. I want to get the event in the code. How to do that?

Also, the binding has some problem. If I press down arrows to focus a state, but if I don't press enter and click elsewhere to dismiss the list, the state still appears on the right. This is not what I expected. If you do the same thing on Google, you will see that it does not start searching.

So, is it currently possible to get an event for the "focus -> enter key" event? Or is it it not possible and the project needs updates for that?

HubKing avatar Oct 27 '21 11:10 HubKing

PropertyChanged binding update means every time the item is modified the event is fired. There is no SelectedItemChanged event per tickets #15 and #2. There isn't really a reason said event couldn't be added pretty easily. You can do a XAML binding in code however (so no xaml needed) that may work until/if a selecteditemchanged event occurs.

mitchcapper avatar Oct 28 '21 19:10 mitchcapper

Sorry, to your other question of when you click to dismiss that is as intended. The cases where it normally gets cancelled(not updated) are: https://github.com/quicoli/WPF-AutoComplete-TextBox/blob/89f82f8e4bc136a541e67ac18a7a42b8ea16eb87/AutoCompleteTextBox/AutoCompleteTextBox/Editors/SelectionAdapter.cs#L45-L69

Otherwise when the popup is closed it is comitted (changed): https://github.com/quicoli/WPF-AutoComplete-TextBox/blob/89f82f8e4bc136a541e67ac18a7a42b8ea16eb87/AutoCompleteTextBox/AutoCompleteTextBox/Editors/AutoCompleteTextBox.cs#L373-L379

it is possible this could exposed as a user preference to if dismissing the window selects or cancels.

mitchcapper avatar Oct 28 '21 19:10 mitchcapper

@HubKing #50 got merged in recently and provides you with the opportunity to do what you mentioned. It isn't directly meant for all your requests but I think it can do both.

It added a PreSelectionAdapterFinish event that fires off the two controls themselves. The event not only gives you notification on changes (your first request) but allows you to cancel out the default behavior saying you are handling it yourself. It provides the reason from the selection change, so if the reason for the change is popup closed (user clicking off control) you can subscribe to that event, set the handled arg and then do as you like. Please note setting that argument means you are saying you are handling it, and the normal code execution path that follows is stopped so you may want to review what would normally happen next: https://github.com/quicoli/WPF-AutoComplete-TextBox/blob/3dc290fcfe784d4d97069e0941d2a09a5bde8c7c/AutoCompleteTextBox/AutoCompleteTextBox/Editors/AutoCompleteTextBox.cs#L431-L440

and https://github.com/quicoli/WPF-AutoComplete-TextBox/blob/3dc290fcfe784d4d97069e0941d2a09a5bde8c7c/AutoCompleteTextBox/AutoCompleteTextBox/Editors/AutoCompleteTextBox.cs#L455-#L466

To do as you mentioned of cancelling on focus leave, I believe just setting SelectedItem to null on the event, and then you don't even need to set it to handled (as the default code path will do what you want).

You can also see an inverse example in the original PR #50 of where no item is selected and if there is only one item in the dropdown and the user hit enter I select it for them and then let the default handler continue.

mitchcapper avatar Mar 08 '22 22:03 mitchcapper