我们需要使用从数据库中检索到的数据填充组合框。因为检索到的潜在记录数可能有数千个,所以我们通过在用户输入组合框中的前 5 个字符之前甚至不调用数据库来限制列表。然后使用 combobox.Items.Filter 填充列表并作为用户键在附加字符中逐步过滤。
问题是这个过程是不可逆的。假设用户输入了 000-Test11,显示的是 000-Test11 和 000-Test111。如果用户按退格键,组合框应返回 000-Test1 并显示上述内容以及 000-Test12、000-Test13 等。
组合框文本编辑器文本更改事件中的逻辑已改编自 MSDN 线程 - 实现自动完成组合框 - 赖以灵,Rovi Corporation 答案:http ://social.msdn.microsoft.com/Forums/en-US/wpf/thread /cec1b222-2849-4a54-bcf2-03041efcf304/。
这是演示我们遇到的问题的代码。
StudyProxy 类:
namespace WPFTesting
{
public class StudyProxy
{
public int StudyID { get; set; }
public string StudyNumber { get; set; }
public string Title { get; set; }
public StudyProxy Init()
{
this.Title = this.StudyNumber;
return this;
}
}
}
xml:
<Window x:Class="WPFTesting.SelectStudyScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" SizeToContent="WidthAndHeight">
<Grid>
<DockPanel Height="250" Width="250">
<ComboBox DockPanel.Dock="Top" VerticalAlignment="Top" Name="comboBox1"
IsEditable="True" IsReadOnly="False" Margin="10"
DisplayMemberPath="StudyNumber" SelectedValuePath="StudyID"
SelectionChanged="comboBox1_SelectionChanged"/>
<DataGrid Name="dataGrid1" />
</DockPanel>
</Grid>
</Window>
后面的代码:
using System;
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls.Primitives;
using System.Runtime.Serialization;
namespace WPFTesting
{
/// <summary>
/// Interaction logic for SelectStudyScreen.xaml
/// </summary>
public partial class SelectStudyScreen
{
private Popup _comboBox1Popup;
private TextBox _comboBox1Editor;
private StudyProxy _currentItem;
public SelectStudyScreen()
{
InitializeComponent();
comboBox1.Loaded += new RoutedEventHandler(comboBox1_Loaded);
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBox1.SelectedIndex == -1)
{
dataGrid1.ItemsSource = null;
}
else
{
_currentItem = comboBox1.SelectedItem as StudyProxy;
List<string> studyIDs = new List<string>(new string[] { comboBox1.SelectedValue.ToString() });
}
}
private void comboBox1_Loaded(object sender, RoutedEventArgs e)
{
_comboBox1Popup = comboBox1.Template.FindName("PART_Popup", comboBox1) as Popup;
_comboBox1Editor = comboBox1.Template.FindName("PART_EditableTextBox", comboBox1) as TextBox;
if (_comboBox1Editor != null)
{
_comboBox1Editor.KeyDown += new System.Windows.Input.KeyEventHandler(comboBox1Editor_KeyDown);
_comboBox1Editor.TextChanged += new TextChangedEventHandler(comboBox1Editor_TextChanged);
_comboBox1Editor.PreviewKeyDown += new KeyEventHandler(comboBox1Editor_PreviewKeyDown);
}
}
void comboBox1Editor_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (_comboBox1Editor.Text != comboBox1.Text)
{
}
}
void comboBox1Editor_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
}
private void comboBox1Editor_TextChanged(object sender, TextChangedEventArgs e)
{
string text = (sender as TextBox).Text.Trim();
if (text.Length < 5)
{
_comboBox1Popup.IsOpen = false;
}
else if (text.Length == 5)
{
_comboBox1Popup.IsOpen = false;
comboBox1.ItemsSource = GetTestData();
}
else
{
// Adapted
// From: Implimenting AutoComplete combobox - Yiling Lai, Rovi Corporation answer
// Link: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/
comboBox1.Items.Filter += a =>
{
if ((a as StudyProxy).StudyNumber.StartsWith(text, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
};
_comboBox1Popup.IsOpen = true;
}
}
private List<StudyProxy> GetTestData()
{
List<StudyProxy> list = new List<StudyProxy>();
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test1" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test11" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test111" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test1111" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test12" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test122" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test1222" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test13" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test133" }.Init());
list.Add(new StudyProxy() { StudyID = 1, StudyNumber = "000-Test1333" }.Init());
return list;
}
}
}