轉(zhuǎn)帖|其它|編輯:郝浩|2011-04-07 13:47:45.000|閱讀 724 次
概述:自動完成輸入框控件AutoCompleteBox是一種很常用的控件,它實現(xiàn)了文本框的輸入的自動搜索的功能,可以加快用戶的輸入效率。該控件在微軟的 Silverlight 開源控件項目“Silverlight Toolkit”中提供了,所以要在Windows Phone 7的應(yīng)用程序里面要使用這樣的一個控件就需要需要引入Toolkit組件,即要加載 Microsoft.Phone.Controls.Toolkit.dll的引用。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
自動完成輸入框控件AutoCompleteBox是一種很常用的控件,它實現(xiàn)了文本框的輸入的自動搜索的功能,可以加快用戶的輸入效率。該控件在微軟的Silverlight 開源控件項目“Silverlight Toolkit”中提供了,所以要在Windows Phone 7的應(yīng)用程序里面要使用這樣的一個控件就需要需要引入Toolkit組件,即要加載Microsoft.Phone.Controls.Toolkit.dll的引用。
下面通過兩種方式來在Windows Phone 7應(yīng)用程序上實現(xiàn)AutoCompleteBox控件:
第一種方式:直接的code-behind用List綁定控件
第二種方式:使用MVVM模式用面向?qū)ο蟮姆绞浇壎丶?/p>
模糊查詢功能使用了第一種方式
前綴查詢功能使用的是第二種方式
<phone:PhoneApplicationPage
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "//schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone= "clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:toolkit= "clr-namespace:Microsoft.Phone.Controls;assembly
=Microsoft.Phone.Controls.Toolkit"
xmlns:shell= "clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d= "//schemas.microsoft.com/expression/blend/2008"
xmlns:mc= "//schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls= "clr-namespace:Microsoft.Phone.Controls;assembly
=Microsoft.Phone.Controls"
x:Class= "TestingAutoComplete.MainPage"
xmlns:My= "clr-namespace:TestingAutoComplete"
mc:Ignorable= "d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily= "{StaticResource PhoneFontFamilyNormal}"
FontSize= "{StaticResource PhoneFontSizeNormal}"
Foreground= "{StaticResource PhoneForegroundBrush}"
SupportedOrientations= "PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible= "True">
<phone:PhoneApplicationPage.Resources>
<My:Names x:Key="Names"></My:Names>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<controls:Pivot Title="AutoCompleteBox控件">
<controls:PivotItem Header="模糊" Margin="0">
<Grid>
<toolkit:AutoCompleteBox Grid.Row="0"
x:Name= "people1" Height="70"/>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="前綴" Margin="0">
<Grid DataContext="{StaticResource Names}">
<toolkit:AutoCompleteBox
Name= "people2"
ValueMemberBinding= "{Binding MyName}"
ItemsSource= "{ Binding ListOfNames}" >
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyName}"></TextBlock>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</Grid>
</controls:PivotItem>
</controls:Pivot>
</Grid>
</phone:PhoneApplicationPage>
注意:
第二種方式的實現(xiàn)添加了下面的引用和綁定
xmlns:My="clr-namespace:TestingAutoComplete"
<phone:PhoneApplicationPage.Resources>
<My:Names x:Key="Names"></My:Names>
</phone:PhoneApplicationPage.Resources>
<Grid DataContext="{StaticResource Names}">
<toolkit:AutoCompleteBox
Name="people2"
ValueMemberBinding="{Binding MyName}"
ItemsSource="{ Binding ListOfNames}" >
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyName}"></TextBlock>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</Grid>
下面的MainPage.xaml.cs代碼是給第一種方式使用的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;
namespace TestingAutoComplete
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
List <string> names = new List<string>();
string namesString = "Fernando Sucre,Scofield,Alexander Mahone,
Theodore Bagwell,Sara Tancredi ,Lincoln Burrows,John Abruzzi,Fluorine";
foreach (var name in namesString.Split(','))
names.Add(name);
this.people1.ItemsSource = names;
this.people1.ItemFilter += SearchCountry;
}
//全局模糊搜索
bool SearchCountry(string search, object value)
{
if (value != null)
{
//如果包含了搜索的字符串則返回true
if (value.ToString().ToLower().IndexOf(search) >= 0)
return true;
}
// 如果不匹配 返回false
return false;
}
}
}
第二種方式使用的類
Name.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace TestingAutoComplete
{
public class Name
{
public string MyName { get; set; }
public override string ToString()
{
return MyName;
}
}
}
Names.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace TestingAutoComplete
{
public class Names : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection <Name> _listOfnames;
public ObservableCollection <Name> ListOfNames
{
get { return _listOfnames; }
set
{
_listOfnames = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs( "ListOfNames"));
}
}
public Names()
{
ListOfNames = new ObservableCollection <Name>();
string namesString = "Fernando Sucre,Scofield,Alexander Mahone,
Theodore Bagwell,Sara Tancredi ,Lincoln Burrows,John Abruzzi,Fluorine";
foreach (var name in namesString.Split(','))
ListOfNames.Add(new Name() { MyName = name });
}
}
}
AutoCompleteBox控件常用方法:
PopulateComplete:通知AutoCompleteBox,ItemsSource屬性已經(jīng)確定,數(shù)據(jù)可以被過濾用以在下拉框中提供可能合適的匹配選項。
組件常用屬性:
FilterMode:獲取或設(shè)置文本框中的文本怎樣被用來過濾具體由為下拉內(nèi)容準備的ItemsSource屬性的項。
IsDropDownOpen:獲取或設(shè)置一個值用以確定該組件的下拉部分是否已打開。
IsTextCompletionEnabled:該屬性的作用是獲取或設(shè)置一個值用以確定在過濾過程中第一個可能的匹配結(jié)果是否自動地被填充至
AutoCompleteBox組件中。有時我們需要第一個匹配的預選對象自動填充至AutoCompleteBox的文本框中,這時就需要通過設(shè)置該屬
性以達到所需效果。
ItemFilter:獲取或設(shè)置自定義方法用來使用用戶輸入的文本來過濾在下拉框中顯示的具體由ItemsSource屬性所決定的items。
MaxDropDownHeight:獲取或設(shè)置該組件的下拉部分高度的最大值。
MinimumPopulateDelay:獲取或設(shè)置第一個匹配結(jié)果出現(xiàn)的最小延遲時間。
MinimumPrefixLength:獲取或設(shè)置需要被鍵入該組件文本框中的最小字符數(shù)量,在該組件顯示可能的匹配之前。
SearchText:獲取在itemsSource項目集合中被用作過濾項目的文本。
SelectionAdapter:獲取或設(shè)置用以生成下拉選項部分的選擇適配器,通過一個可選擇的項目列表。
Text:獲取或設(shè)置該組件文本框中的值。
TextBoxStyle:獲取或設(shè)置該組件的文本框的樣式。
TextFilter:獲取或設(shè)置自定義方法用來使用用戶輸入的文本以基于文本的方式過濾在下拉框中顯示的具體由ItemsSource屬性所決
定的items。
ValueMemberBinding:獲取或設(shè)置用于獲取對在AutoCompleteBox控制文本框部分顯示的值的綁定,為顯示在下拉過濾項目。
ValueMemberPath:獲取或設(shè)置用來獲取為在AutoCompleteBox控制文本框部分顯示的值的屬性的路徑,為顯示在下拉篩選項目。
組件常用事件:
DropDownClosed:發(fā)生于IsDropDownOpen屬性由true轉(zhuǎn)為false。
DropDownClosing:發(fā)生于IsDropDownOpen屬性正在由true轉(zhuǎn)為false。
DropDownOpened:發(fā)生于IsDropDownOpen屬性由false轉(zhuǎn)為true。
DropDownOpening:發(fā)生于IsDropDownOpen屬性正在由false轉(zhuǎn)為true。
Populated:發(fā)生于當該組件已經(jīng)生成匹配Text屬性的下拉部分時。
Populating:發(fā)生于當該組件正在生成匹配Text屬性的下拉部分時。
SelectionChanged:發(fā)生于當該組件的下拉部分的選項改變時。
TextChanged:發(fā)生于當該組件文本框中的值改變時。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園