Skip to content

[Feature] Ability for TokenizingTextBox to keep the typed text after submitting or selecting #168

Open
CommunityToolkit/WindowsCommunityToolkit
#4922
@smumovic

Description

@smumovic

Describe the problem

I'm using the TokenizingTextBox to implement an advanced quick search in my application. When a user starts typing, they first get a list of fields they can search through, and when selecting one the selected field appears as a token in the search box letting the user know they are searching only for that field. They can select several fields to search through all the selected ones.

The trouble is that every time the TTB is submitted (by pressing enter or by selecting an item from the suggestions) the Text property is emptied. I have to resort to cheating it via "TokenItemAdding" and "GotFocus" events (to store the text in a temp string, and restore it after the "QuerySubmitted" event has executed, as I noticed in the source code for the TTB that the QuerySubmitted event actually empties the Text property.

Describe the solution

I found the exact place the TTB empties the Text field (lines 156 and 157 of the TokenizingTextBoxItem.cs):

QuerySubmittedMethod

My proposal is to surround the two lines that empty the text with a conditional statement dependent on a top-level property that can be set in XAML, something like KeepTextInputAfterQuerySubmit defaulted to false to keep it backward compatible.

I tested it in a branch ad it behaves exactly like I need it to behave (I commented out the two lines to get a quick feel).

I can put in the work to properly introduce the property in the XAML and all the in-between code in order to make it proper and submit a PR. I just need acceptance by the community to get the PR approved.

Alternatives

One alternative is to introduce a PostQuerySubmitted event that i can use to restor the text box content. But this is less elegant and there will be a slight delay when the filed will be empty.

Yet another alternative is to enable the component to be extended and the original QuerySubmitted method overridden, but this is much more trouble than its worth IMHO.

Additional info

This is an example from my modified component that keeps the text:

ExampleOfTtbKeepingTheText

I achieved this by commenting out the two lines and testing it in the Sample App. The text is kept in the text box after typing and selecting one of the offered options. It creates the token, but the text can be used to refine the search after the results have been loaded (in my use case).

Help us help you

Yes, I'd like to be assigned to work on this item.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions