AutoSuggestBox class
TOC
Collapse the table of content
Expand the table of content

AutoSuggestBox class

Represents a text control that makes suggestions to users as they type. The app is notified when text has been changed by the user and is responsible for providing relevant suggestions for this control to display.

Inheritance

Object
  DependencyObject
    UIElement
      FrameworkElement
        Control
          ItemsControl
            AutoSuggestBox

Syntax


public sealed class AutoSuggestBox : ItemsControl


<AutoSuggestBox .../>

Members

The AutoSuggestBox class has these types of members:

Constructors

The AutoSuggestBox class has these constructors.

ConstructorDescription
AutoSuggestBox Initializes a new instance of the AutoSuggestBox class.

 

Events

The AutoSuggestBox class has these events.

EventDescription
QuerySubmitted Occurs when the user submits a search query.
SuggestionChosen Raised before the text content of the editable control component is updated.
TextChanged Raised after the text content of the editable control component is updated.

 

Properties

The AutoSuggestBox class has these properties.

PropertyAccess typeDescription

AutoMaximizeSuggestionArea

Read/write

Indicates if the suggestion area should be automatically maximized.

AutoMaximizeSuggestionAreaProperty

Read-only

Identifies the AutoMaximizeSuggestionArea dependency property.

Header

Read/write

Gets or sets the header object for the text box portion of this control.

HeaderProperty

Read-only

Identifies the Header dependency property.

IsSuggestionListOpen

Read/write

Gets or sets a Boolean value indicating whether the drop-down portion of the AutoSuggestBox is open.

IsSuggestionListOpenProperty

Read-only

Identifies the IsSuggestionListOpen dependency property.

LightDismissOverlayMode

Read/write

Gets or sets a value that specifies whether the area outside of a light-dismiss UI is darkened.

LightDismissOverlayModeProperty

Read-only

Identifies the LightDismissOverlayMode dependency property.

MaxSuggestionListHeight

Read/write

Gets or set the maximum height for the drop-down portion of the AutoSuggestBox control.

MaxSuggestionListHeightProperty

Read-only

Identifies the MaxSuggestionListHeight dependency property.

PlaceholderText

Read/write

Gets or sets the placeholder text to be displayed in the control.

PlaceholderTextProperty

Read-only

Identifies the PlaceholderText dependency property.

QueryIcon

Read/write

Gets or sets the graphic content of the button that is clicked to initiate a query.

QueryIconProperty

Read-only

Identifies the QueryIcon dependency property.

Text

Read/write

Gets or sets the text that is shown in the control.

TextBoxStyle

Read/write

Gets or sets the style of the auto-suggest text box.

TextBoxStyleProperty

Read-only

Identifies the TextBoxStyle dependency property.

TextMemberPath

Read/write

Gets or sets the property path that is used to get the value for display in the text box portion of the AutoSuggestBox control, when an item is selected.

TextMemberPathProperty

Read-only

Identifies the TextMemberPath dependency property.

TextProperty

Read-only

Identifies the PlaceholderText dependency property.

UpdateTextOnSelect

Read/write

Used in conjunction with TextMemberPath, gets or sets a value indicating whether items in the view will trigger an update of the editable text part of the AutoSuggestBox when clicked.

UpdateTextOnSelectProperty

Read-only

Identifies the UpdateTextOnSelect dependency property.

 

Remarks

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.

Examples

To see a complete working example of AutoSuggestBox, see the AutoSuggestBox migration sample.

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.
    }
}

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

None supported

Minimum supported server

None supported

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.winmd

Attributes

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version]
[WebHostHidden()]

See also

Object
ItemsControl
IItemContainerMapping
AutoSuggestBox styles and templates
AutoSuggestBox migration sample (Windows 10)

 

 

Show:
© 2016 Microsoft