Strange results in AutoSuggestBox on Windows Phone 8.1

I'm trying to use the standard AutoSuggestBox in the XAML app for Windows Phone 8.1, but it behaves very weird.

In a simple demo, I have a collection

 Items = new ObservableCollection<string> { "a", "b", "c", "d" }; 

and he has an AutoSuggestBox in XAML:

 <AutoSuggestBox ItemsSource="{Binding Items}" /> 

The problem is that no matter what I write in the AutoSuggestBox , I always get all the elements:

enter image description here

The documentation says almost nothing, and I did not find any samples using this control.

+5
source share
2 answers

Based on this blog post , it seems that what you expect (automatic filtering) is not the case - instead, you need to connect to the TextChanged event and populate the Suggestions collection yourself.

From the documentation :

The application is notified when the text has been changed by the user and is responsible for providing appropriate suggestions for displaying this control.

+4
source

Try using the following code:

  private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) { List<string> myList = new List<string>(); foreach (string myString in PreviouslyDefinedStringArray) { if (myString.Contains(sender.Text) == true) { myList.Add(myString); } } sender.ItemsSource = myList; } 

This should work on WP 8.1

+6
source

Source: https://habr.com/ru/post/1205799/


All Articles