当前位置:首页 > Windows程序 > 正文

【Win 10开发】关于AutoSuggestBox

2021-03-22 Windows程序

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

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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/65448.html