Silverlight中AutoCompletTextBox的使用
<Grid x:Name="LaoutRoot">
<StackPanel Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="书籍名称" Width="50" VerticalAlignment="Center" Margin="380,0,15,0"></TextBlock>
<input:AutoCompleteBox Width="150" x:Name="AutoBookName"
IsTextCompletionEnabled="false" IsTabStop="True"
Populating="AutoBookName_Populating">
</input:AutoCompleteBox>
<Button x:Name="BtnSearch" Content="搜索" Width="100" Margin="15,0,15,0" VerticalAlignment="Center" Click="BtnSearch_Click"></Button>
</StackPanel>
</Grid>
关于页面设置比较简单,主要的就是该控件的几个比较重要的属性和事件:
利用AutoCompleteBox时需注意该组件的事件发生在Populating上
IsTextCompletionEnabled 属性则表示搜索的数据中第一条是否显示在AutoCompleteBox文本框中
后台代码:
/// <summary>
/// 此方法为autocompletebox绑定数据
/// 事件:Populating下发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AutoBookName_Populating(object sender, PopulatingEventArgs e)
{
AutoCompleteBox source = sender as AutoCompleteBox; //得到当前触发事件的AutoCompleteBox
//调用异步数据查询
SearchBookByName(source.Text.ToString().Trim());
}
public void SearchBookByName(string bookName)
{
//调用相对应的webservice服务
BookService.BooksServiceSoapClient client = new MyBookShop.BookService.BooksServiceSoapClient();
client.FindByBookNameCompleted += new EventHandler<MyBookShop.BookService.FindByBookNameCompletedEventArgs>(client_FindByBookNameCompleted);
client.FindByBookNameAsync(bookName);
}
/// <summary>
/// 异步数据查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_FindByBookNameCompleted(object sender, MyBookShop.BookService.FindByBookNameCompletedEventArgs e)
{
//绑定AutoCompleteBox数据
if (AutoBookName != null && e.Error == null && !e.Cancelled)
{
if (e.Result.Count > 0)
{
List<string> employeeStrList = new List<string>();
foreach (BookService.Book book in e.Result)
{
employeeStrList.Add(book.Title.Trim());
}
//绑定数据源
AutoBookName.ItemsSource = employeeStrList;
AutoBookName.PopulateComplete();
}
}
}