【Win10开发】关于AutoSuggestBox

其实看名字我们就知道,这个控件可以提供一些建议文本。我们在做搜索框时可以做一些文本来让用户选择。

这个控件有两个关键的事件QuerySubmittedSuggestionChosen事件,当下拉列表中的项被选择后,会发生SuggestionChosen事件。当在下拉列表中做出选择后会提交输入的文本,这时候会发生QuerySubmitted事件,从事件参数的QueryText属性可以获取输入框中要提交的查询文本。


我们做个查询单词的小例子吧,首先看看效果。

那么,现在先进行xaml布局。

1         <StackPanel HorizontalAlignment="Center" Width="400">
2             <AutoSuggestBox Name="search" QueryIcon="Find" PlaceholderText="请输入一个单词"
3                         QuerySubmitted="search_QuerySubmitted" SuggestionChosen="search_SuggestionChosen">
4             </AutoSuggestBox>
5             <TextBlock Name="content" FontSize="40" Foreground="LightBlue"/>
6         </StackPanel>  

 接下来,我们为AutoSuggestBox控件设置下拉列表,用ItemsSource属性来指定数据源。

        string[] array = new string[]
        {
                "auto","suggest","search","page","surface","microsoft","keyboard","help"
        };
        ...

        search.ItemsSource = array;

处理SuggestionChosen事件

1         private void search_SuggestionChosen(Windows.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
2         {
3             string s = args.SelectedItem as string;
4             sender.Text = s;
5         }

处理QuerySubmitted事件

        private void search_QuerySubmitted(Windows.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
        {
            content.Text = $"{args.QueryText}";
        }

 至此,这个小例子就完成了。

posted @ 2015-11-26 22:25  Silence.Sky  阅读(481)  评论(0编辑  收藏  举报