Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange results in AutoSuggestBox in Windows Phone 8.1

I am trying to use the standard AutoSuggestBox in a Windows Phone 8.1 XAML app, but it behaves really strangely.

In a simple demo, I have collection

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

and he AutoSuggestBox in XAML:

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

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

enter image description here

The documentation says next to nothing and I have not found any samples using this control.

like image 988
Igor Kulman Avatar asked Oct 29 '14 17:10

Igor Kulman


2 Answers

Try 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

like image 89
Anmar Qashqish Avatar answered Sep 23 '22 13:09

Anmar Qashqish


Based on this blog post, it looks like what you're expecting (automatic filtering) isn't the case - instead, you need to hook into the TextChanged event and populate the Suggestions collection yourself.

From the documentation:

The app is notified when text has been changed by the user and is responsible for providing relevant suggestions for this control to display.

like image 29
Jon Skeet Avatar answered Sep 22 '22 13:09

Jon Skeet