Silverlight入门:第五部分 - 整合其它控件2010-12-10 博客园 焦炜在之前的章节中,我们优化了数据绑定,并将数据保存在了我们的独立存储 区域。现在让我们整合一些其它的控件使得用户体验更好一些。自动完成输入框记得每次搜索以后,我们都将搜索条件保存为历史数据吗?让我们通过在他 们输入时提供搜索历史来让他们使用更方便。我们准备用Silverlight工具包中 的一个控件——自动完成输入框来做这件事。要做到这点,我们需要添加一个到System.Windows.Controls.Input的程序集 引用,然后在你的Search.xaml文件中添加一个xmlns:
1xmlns:input="clr- namespace:System.Windows.Controls;assembly=System.Windows.Controls.Inp ut"
我们还要在这里将自动完成输入框的Name属性改为SearchTerm:
1 <input:AutoCompleteBox x:Name="SearchTerm" FontSize="14.667" Margin="0,0,10,0" Width="275"
2 IsTextCompletionEnabled="True" />
现在我们需要为它提供数据,因此我们要在Helper.cs类中添加以下方法:
1 internal static string[] GetSearchTermHistory()
2 {
3 List<string> searchHistory = new List<string>();
4
5 foreach (var item in IsolatedStorageSettings.ApplicationSettings.Keys)
6 {
7 searchHistory.Add(item.ToString ());
8 }
9
10 return searchHistory.ToArray();
11 }
之后在Search.xaml.cs中的Loaded事件处理器中,我添加了 Helper.GetSearchTermHistory()的调用:
1 void Search_Loaded(object sender, RoutedEventArgs e)
2 {
3 SearchResults.ItemsSource = pcv; // bind the DataGrid
4 _timer.Start(); // start the timer
5 SearchForTweetsEx(); // do the initial search
6 SearchTerm.ItemsSource = Helper.GetSearchTermHistory(); // populate autocomplete
7 }
这个效果就是当程序加载后,当用户输入搜索条件时为他们提供提示:

很有用吧!