Search code examples
windows-phone-8windows-phonewindows-phone-8.1

Autocomplete textbox in windows phone 8.1


I want in my windows phone 8.1 app some feature,that my user input some character and I suggest him some word.

public IEnumerable AutoCompletions = new List<string>()
{
 "Lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "Nullam", "felis", "dui", "gravida", "at"};

for example user input "a" and i suggest "amet","at" and "adipiscing", further user input "am" and i suggest "amet". Help me please


Solution

  • What you want to do is display only the suggestions that apply for the given input. Not all possible strings.

    Lets assume you have the following AutoSuggestBox:

    <AutoSuggestBox 
       TextChanged="AutoSuggestBox_TextChanged"
       SuggestionChosen="AutoSuggestBox_SuggestionChosen">
        <AutoSuggestBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding}" />
                </StackPanel>
            </DataTemplate>
        </AutoSuggestBox.ItemTemplate>
    </AutoSuggestBox>
    

    These are the event handlers:

    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
        {
            if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
            {
                // You can set a threshold when to start looking for suggestions
                if (sender.Text.Length > 3)
                {
                    sender.ItemsSource = getSuggestions(sender.Text); 
                }
                else {
                    sender.ItemsSource = new List<String> { };
                }
            }
        }
    

    All you have to do is to write a getSuggestions(String text) method that returns plausible suggestions for a given input.