Skip to content

Latest commit

 

History

History
180 lines (123 loc) · 10.5 KB

autosuggestbox.md

File metadata and controls

180 lines (123 loc) · 10.5 KB
-api-id -api-type
T:Windows.UI.Xaml.Controls.AutoSuggestBox
winrt class

Windows.UI.Xaml.Controls.AutoSuggestBox

-description

Represents a text control that makes suggestions to users as they enter text using a keyboard or pen (using ink and handwriting recognition). The app is notified when text has been changed by the user and is responsible for providing relevant suggestions for this control to display.

-xaml-syntax

<AutoSuggestBox .../>

-remarks

Tip

For more info, design guidance, and code examples, see Auto-suggest box.

Use an AutoSuggestBox to provide a list of suggestions for a user to select from as they type.

By default, the text entry box doesn’t have a query button shown. You can set the QueryIcon property to add a button with the specified icon on the right side of the text box. For example, to make the AutoSuggestBox look like a typical search box, add a ‘find’ icon, like this.

<AutoSuggestBox QueryIcon="Find"/>

Here's an AutoSuggestBox with a 'find' icon. The suggestion list shows suggested results based on the user's entry.

An auto suggest box with an icon and suggestions

To use an AutoSuggestBox, you need to respond to 3 user actions.
  • Text changed - When the user enters text, update the suggestion list.
  • Suggestion chosen - When the user chooses a suggestion in the suggestion list, update the text box.
  • Query submitted - When the user submits a query, show the query results.

Text changed

The TextChanged event occurs whenever the content of the text box is updated. Use the event args Reason property to determine whether the change was due to user input. If the change reason is UserInput, filter your data based on the input. Then, set the filtered data as the ItemsSource of the AutoSuggestBox to update the suggestion list.

To control how items are displayed in the suggestion list, you can use DisplayMemberPath or ItemTemplate.

  • To display the text of a single property of your data item, set the DisplayMemberPath property to choose which property from your object to display in the suggestion list.
  • To define a custom look for each item in the list, use the ItemTemplate property .

Suggestion chosen

When a user navigates through the suggestion list using the keyboard, you need to update the text in the text box to match.

You can set the TextMemberPath property to choose which property from your data object to display in the text box. If you specify a TextMemberPath, the text box is updated automatically. You should typically specify the same value for DisplayMemberPath and TextMemberPath so the text is the same in the suggestion list and the text box.

If you need to show more than a simple property, handle the SuggestionChosen event to populate the text box with custom text based on the selected item.

Query submitted

Handle the QuerySubmitted event to perform a query action appropriate to your app and show result to the user.

The QuerySubmitted event occurs when a user commits a query string. The user can commit a query in one of these ways:

  • While focus is in the text box, press Enter or click the query icon. The event args ChosenSuggestion property is null.
  • While focus is in the suggestion list, press Enter, click, or tap an item. The event args ChosenSuggestion property contains the item that was selected from the list. In all cases, the event args QueryText property contains the text from the text box.

Accessibility

If you are using an assistive technology, such as Narrator, to interact with the AutoSuggestBox the accessibility experience has already been hooked up for you. A user will:

  • Know the list is present and when the list closes
  • Know how many suggestions are available
  • Be able to move Narrator focus to the list
  • Be able to Navigate through a suggestion with all other reading modes See Auto-suggest accessibility for more information.

Pen input

Starting with Windows 10, version 1803, XAML text input boxes feature embedded support for pen input using Windows Ink. When a user taps into a text input box using a Windows pen, the text box transforms to let the user write directly into it with a pen, rather than opening a separate input panel.

You can configure the handwriting view of the AutoSuggestBox through its TextBox control, as we show here.

<AutoSuggestBox Name="NoHandwritingAutoSuggestBox"
        BorderThickness="0"  Width="680"
        FontSize="24" VerticalAlignment="Top"
        HorizontalAlignment="Center"
        PlaceholderText="Handwriting view is not supported">
    <AutoSuggestBox.TextBoxStyle>
        <Style TargetType="TextBox">
            <Setter Property="IsHandwritingViewEnabled" Value="False"/>
        </Style>
    </AutoSuggestBox.TextBoxStyle>
</AutoSuggestBox>

Text box with ink and suggestions

For more info, see Text input with the handwriting view.

Control style and template

You can modify the default Style and ControlTemplate to give the control a unique appearance. For information about modifying a control's style and template, see Styling controls. The default style, template, and resources that define the look of the control are included in the generic.xaml file. For design purposes, generic.xaml is available locally with the SDK or NuGet package installation.

  • WinUI Styles (recommended): For updated styles from WinUI, see \Users\<username>\.nuget\packages\microsoft.ui.xaml\<version>\lib\uap10.0\Microsoft.UI.Xaml\Themes\generic.xaml.
  • Non-WinUI styles: For built-in styles, see %ProgramFiles(x86)%\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\<SDK version>\Generic\generic.xaml.

Locations might be different if you customized the installation. Styles and resources from different versions of the SDK might have different values.

XAML also includes resources that you can use to modify the colors of a control in different visual states without modifying the control template. Modifying these resources is preferred to setting properties such as Background and Foreground. For more info, see the Light-weight styling section of the XAML styles article. Light-weight styling resources are available starting in Windows 10, version 1607 (SDK 14393).

Resources that start with TextControl are shared by TextBox, PasswordBox, RichEditBox, and AutoSuggestBox. Changes to these resources will affect all four controls.

Version history

Windows version SDK version Value added
1607 14393 LightDismissOverlayMode
1809 17763 Description

-examples

Tip

For more info, design guidance, and code examples, see Auto-suggest box.

[!div class="nextstepaction"] Open the WinUI 2 Gallery app and see the AutoSuggestBox in action

The WinUI 2 Gallery app includes interactive examples of most WinUI 2 controls, features, and functionality. Get the app from the Microsoft Store or get the source code on GitHub.

Here is a simple AutoSuggestBox with the required event handlers.

<AutoSuggestBox PlaceholderText="Search" QueryIcon="Find" Width="200"
                TextChanged="AutoSuggestBox_TextChanged"
                QuerySubmitted="AutoSuggestBox_QuerySubmitted"
                SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    // Only get results when it was a user typing, 
    // otherwise assume the value got filled in by TextMemberPath 
    // or the handler for SuggestionChosen.
    if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
    {
        //Set the ItemsSource to be your filtered dataset
        //sender.ItemsSource = dataset;
    }
}


private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
    // Set sender.Text. You can use args.SelectedItem to build your text string.
}


private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
    if (args.ChosenSuggestion != null)
    {
        // User selected an item from the suggestion list, take an action on it here.
    }
    else
    {
        // Use args.QueryText to determine what to do.
    }
}

-see-also

ItemsControl, IItemContainerMapping, AutoSuggestBox migration sample (Windows 10), Guidelines for auto-suggest boxes, Search and find-in-page