码迷,mamicode.com
首页 > Windows程序 > 详细

【Win 10开发】关于AutoSuggestBox

时间:2015-11-26 22:54:06      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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

这个控件有两个关键的事件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}";
        }

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

【Win 10开发】关于AutoSuggestBox

标签:

原文地址:http://www.cnblogs.com/skyshalo/p/4999230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!