Unable to type in text box on UWP
Description
Using the AutoSuggestBox on UWP and when you start typing, a popup shows matching suggestions, however when the popup(Flyout?) is visible, you can't type in the search/text box. At no time can I type, backspace, etc and get
I do see an X flicker in the textbox sometime, suggesting I can click X to cancel my entry and revert to placeholder text, but I can never click it.
Selecting the item from the suggestion box works.
Steps to Reproduce
Bind to an ItemSource w/ 1 item in it, run it Type in a letter that matches the item Try typing more characters, doing backspace, tab, etc. 2.
Expected Behavior
Actual Behavior
Basic Information
-
Version with issue:
-
Last known good version:
-
IDE: VS 2017
-
Platform Target Frameworks: netstandard2.0
- UWP: min version: Win 10 fall creators (19.0; Build 16299) target version: Win 10, version 1809; build 17763
-
Nuget Packages: Xamarin.Forms, Rg.Plugins.Popup.
-
Affected Devices: My machine.
@wtheronjones I'm not seeing this based on the above reproducer steps. Could you share a sample application?
I will have to get back to you on a sample as we are doing something else on this and are going to come back to it later due to release timelines. I can provide additional details about what I was trying
My XAML was something like this:
<dm:AutoSuggestBox x:Name="SuggestBox" PlaceholderText="Enter a Something" ItemsSource="{Binding MyListOfStrings}"> <dm:AutoSuggestBox.Behaviors> <behaviors:EventToCommandBehavior EventName="TextChanged" EventArgsConverter="{StaticResource AutoSuggestBoxTextChangedEventArgsConverter}" EventArgsConverterParameter="{x:Reference SuggestBox}" Command="{Binding SuggestTextChanged}" /> <dm:AutoSuggestBox.Behaviors> </dm:AutoSuggestBox>
So I have a view model and am binding TextChanged to a Command instead of an event handler, and I use a Converter to convert the AutoSuggestTextBoxTextChangedEventArgs into the value of "SuggestBox.Text", and that converted value goes to my named SuggestTextChanged that takes a string argument. SuggestTextChanged then filters the the "MyListOfStrings" ObservableCollection based on the current text.
I noticed that in Dynamic.xaml.cs, in Text_Changed, you set ItemsSource programmatically and I'm not doing that. I don't test it's user input or not. I'm also not doing anything w/ QuerySelected
perhaps my logic and/or expectations are off.
@wtheronjones What did you do in your view model? I'm curious if there's some circular eventing going on between your view model and view.
In my view, I have a textbox for a subdomain, and a picker for the domain. ie: mysite.somewhere.com. The "mysite" would be suggested w/ autocomplete, and the "somewhere.com" would be in a Picker. When the Picker is changed, the AutoSuggestBox.ItemsSource is updated. The SuggestTextChanged command in my viewmodel just checks to see if the current SuggestBox.Text(passed by argument via the converter) plus something else matches something in another list, enabling the UI to proceed to the next step.
I think the difference between what I'm doing and what your Dynamic sample shows is that you're changing the ItemSource as you type and I'm not changing it.
Ok, I got it basically working and I found a way to prevent typing.
My TextChanged handler had to have the following logic: if Reason = UserInput and Text = null/empty/whitespace, set ItemSource = null If Reason = UserInput and Text has something, set ItemSource = matching items otherwise(Reason=SuggestionChosen|ProgramaticChange), set ItemSource = null.
Sorry, code formatting looks bad. With that logic, it works nicely.
Here is the way to prevent typing in the control:
Start typing(something that'll match a suggestion) Select an item from the popup Click into the field, keyboard focus at end of text(keyboard focus is somewhere else) Backspace a few characters The suggestion popup comes back Hit tab key Hit shift-tab key, Can't type, can't hit escape to dismiss the popup, can't tab out. You can select the suggestion and resume normal use.
Same thing is happening to me. Here's my code:
private void AddressChanged(object sender, AutoSuggestBoxTextChangedEventArgs e)
{
AutoSuggestBox box = sender as AutoSuggestBox;
if (e.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
string text = box.Text;
if (string.IsNullOrWhiteSpace(text))
{
box.ItemsSource = null;
}
else
{
box.ItemsSource = RecentHosts.Where(host => host.StartsWith(text.Trim(), StringComparison.CurrentCultureIgnoreCase)).ToList();
}
}
else
{
box.ItemsSource = null;
}
}
you're changing the ItemSource as you type and I'm not changing it.
If you want to change the list of suggestions, you should be doing that. The control doesn't filter itself. It's up to your handler to do it. This matches the behavior of UWP's AutoSuggestBox.
OK, I found a workaround, which was to add an Unfocused event listener to explicitly clear ItemsSource when the box loses focus. However, this means that the suggestion list will not be open the next time I focus on the AutoSuggestBox again. If I edit the text after that, it reappears.
Ideally, the suggestion list should appear on focus if there is text. I suspect that's something you went for, but it's broken. The AutoSuggestBox gains focus only to then immediately lose it, creating the issue.
Interestingly, the workaround also works if it's a Focused event listener that clears ItemsSource instead. Hmmm...
Hi, I have the same issue, someone found a solution ?
@JordanLongstaff In my case setting the ItemsSource to null in Unfocused event does work, but the first time i click on the AutoSuggestBox, it loses focus instantly. So you have to click twice to make an input. Setting the ItemsSource to null in Focused event works very good without having to click twice.