更改 AutoCompleteBox 控件的外观和行为

更改 AutoCompleteBox 的外观

文本框部分的外观
<Style x:Key="myTBStyle" TargetType="TextBox">
<Setter Property="Background" Value="LightYellow" />
<Setter Property="Foreground" Value="DarkSlateGray" />
<Setter Property="Margin" Value="5" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="14" />
<Setter Property="BorderBrush" Value="DarkGray" />
</Style>
下拉列表部分
<Style x:Key="myLBStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Khaki" />
<Setter Property="Foreground" Value="DarkSlateGray" />
<Setter Property="Margin" Value="5" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="FontSize" Value="14" />
<Setter Property="BorderBrush" Value="DarkGray" />
</Style>
<sdk:AutoCompleteBox Width="150" Height="30" x:Name="myACB"
TextBoxStyle
="{StaticResource myTBStyle}"
ItemContainerStyle
="{StaticResource myLBStyle}" />

更改 AutoCompleteBox 筛选项的方式

  • FilterMode 属性设置为一个 AutoCompleteFilterMode
<input:AutoCompleteBox FilterMode="ContainsCaseSensitive"
Height
="40" Width="200" x:Name="ACB" />

创建自定义字符串筛选器

TextFilter 属性设置为与接受某个字符串的 AutoCompleteFilterPredicate<(Of <(<'T>)>)> 委托类型的签名相匹配的方法

View Code
List<Employee> employees = new List<Employee>();
public MainPage()
{
InitializeComponent();
// Add some items to the employee list.
employees.Add(new Employee("Sells", "Chris", "csells", 1234));
employees.Add(
new Employee("Cabatana", "Reina", "rcaba", 5678));
employees.Add(
new Employee("Sprenger", "Christof", "cspreng", 9123));
employees.Add(
new Employee("Brandel", "Jonas", "jbrandel", 4567));
employees.Add(
new Employee("Bye", "Dennis", "dbye", 8912));
employees.Add(
new Employee("Reid", "Miles", "mreid", 3456));

// Set the item source.
myACB.ItemsSource = employees;


...


// Set the TextFilter property to the search method.
myACB.TextFilter += SearchEmployees;


...


}


...


bool SearchEmployees(string search, string value)
{
value
= value.ToLower();
// Split the string a new line.
string[] words = value.Split(System.Environment.NewLine.ToCharArray(),
StringSplitOptions.RemoveEmptyEntries);

string[] names = words[0].Split(' ');

// Look for a match in the first line; discarding the "employee:" entry.
foreach (string name in names)
{
if (name != "employee:")
if (name.StartsWith(search))
return true;
}
// If no match, return false.
return false;
}


...


public class Employee
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string EmailName { get; set; }
public int ID { get; set; }
public Employee(string empLastName, string empFirstName, string empEmail, int empID)
{
LastName
= empLastName;
FirstName
= empFirstName;
EmailName
= empEmail;
ID
= empID;
}
public override string ToString()
{
return "Employee: " + FirstName + " " +
LastName
+ System.Environment.NewLine +
"Email: " + EmailName + System.Environment.NewLine + "ID: " +
ID.ToString();
}
}
<StackPanel x:Name="LayoutRoot" Background="LightGray">
<TextBlock FontWeight="Bold" Text="AutoCompleteBox Custom Filter Example" Margin="5"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Employee:" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<sdk:AutoCompleteBox Height="75" Width="200" VerticalAlignment="Center" HorizontalAlignment="Right"
x:Name
="myACB" FilterMode="Custom" ToolTipService.ToolTip="Enter employee name."/>
</StackPanel>
</StackPanel>

创建自定义对象筛选器

ItemFilter 属性设置为与接受某个对象的 AutoCompleteFilterPredicate<(Of <(<'T>)>)> 委托类型的签名相匹配的方法

View Code
List<Employee> employees = new List<Employee>();
public MainPage()
{
InitializeComponent();
// Add some items to the employee list.
employees.Add(new Employee("Sells", "Chris", "csells", 1234));
employees.Add(
new Employee("Cabatana", "Reina", "rcaba", 5678));
employees.Add(
new Employee("Sprenger", "Christof", "cspreng", 9123));
employees.Add(
new Employee("Brandel", "Jonas", "jbrandel", 4567));
employees.Add(
new Employee("Bye", "Dennis", "dbye", 8912));
employees.Add(
new Employee("Reid", "Miles", "mreid", 3456));

// Set the item source.
myACB.ItemsSource = employees;


...


// Set the ItemFilter property to the search method.
myACB.ItemFilter += SearchEmployees;


...


}


...


bool SearchEmployees(string search, object value)
{
// Cast the value to an Employee.
Employee emp = value as Employee;
if (emp != null)
{
// Look for a match in the first and last names.
if (emp.LastName.ToLower().StartsWith(search))
return true;

else if (emp.FirstName.ToLower().StartsWith(search))
return true;
}
// If no match, return false.
return false;
}


...


public class Employee
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string EmailName { get; set; }
public int ID { get; set; }
public Employee(string empLastName, string empFirstName, string empEmail, int empID)
{
LastName
= empLastName;
FirstName
= empFirstName;
EmailName
= empEmail;
ID
= empID;
}
public override string ToString()
{
return "Employee: " + FirstName + " " +
LastName
+ System.Environment.NewLine +
"Email: " + EmailName + System.Environment.NewLine + "ID: " +
ID.ToString();
}
}
<StackPanel x:Name="LayoutRoot" Background="LightGray">
<TextBlock FontWeight="Bold" Text="AutoCompleteBox Custom Filter Example" Margin="5"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Employee:" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<sdk:AutoCompleteBox Height="75" Width="200" VerticalAlignment="Center" HorizontalAlignment="Right"
x:Name
="myACB" FilterMode="Custom" ToolTipService.ToolTip="Enter employee name."/>
</StackPanel>
</StackPanel>

手动填充 AutoCompleteBox 下拉列表

  1. 还可以选择将 MinimumPopulateDelay 设置为 100 或更多,以延迟填充操作。这会减少当用户键入时填充 AutoCompleteBox 的调用次数。

  2. 还可以选择将 MinimumPrefixLength 设置为大于 1 的值,以减少填充操作的调用次数。

  3. 通过将 PopulatingEventArgs..::..Cancel 属性设置为 true,处理 Populating 事件并取消它。这将通知控件您将手动填充它。

  4. 调用将执行筛选操作的方法。如果您调用 Web 服务,这将涉及一个回调方法。在该方法中,当完成筛选操作时,将调用 PopulateComplete。这将通知控件您已完成了填充该控件的过程。

View Code
// Handle the Populating event, but cancel it. Make the call to the web service.
private void AutoCompleteBox_Populating(object sender, PopulatingEventArgs e)
{
e.Cancel
= true;
CallToWebService();
}

// Call the topic service at the live search site.
WebClient wc;
private void CallToWebService()
{
wc
= new WebClient();
wc.DownloadStringAsync(
new Uri
(
"http://api.search.live.net/qson.aspx?query=" + myACB.SearchText +
"&sources=news"));
wc.DownloadStringCompleted
+=
new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}

void wc_DownloadStringCompleted(object sender,
DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}

List
<string> data = new List<string>();
try
{
JsonObject jso
= ((JsonObject)JsonObject.Parse(e.Result))
[
"SearchSuggestion"] as JsonObject;
string originalSearchString = jso["Query"];
if (originalSearchString == myACB.SearchText)
{
foreach (JsonObject suggestion in (JsonArray)jso["Section"])
{
data.Add(suggestion.Values.First());
}

// Diplay the AutoCompleteBox drop down with any suggestions
myACB.ItemsSource = data;
myACB.PopulateComplete();
}
}
catch { }
}
}
<StackPanel x:Name="LayoutRoot" Background="White" Orientation="Horizontal">
<TextBlock Text="News Topic:" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<sdk:AutoCompleteBox VerticalAlignment="Center" HorizontalAlignment="Right" Height="30"
Width
="100" MinimumPopulateDelay="200" MinimumPrefixLength="3" x:Name="myACB" Populating="AutoCompleteBox_Populating" />
</StackPanel>

posted @ 2011-03-15 14:35  南宫元耘  阅读(472)  评论(0编辑  收藏  举报