ComboBox具有渐进式自动完成功能

时间:2012-05-18 10:41:51

标签: wpf autocomplete combobox

我们需要使用从数据库中检索的数据填充组合框。由于检索到的潜在记录数量可能是数千,因此我们限制列表甚至不会调用数据库,直到用户输入组合框中的前5个字符。然后填充列表并逐步过滤,因为用户使用combobox.Items.Filter键入其他字符。

问题是该过程不可逆转。假设用户已输入000-Test11,显示的是000-Test11和000-Test111。如果用户命中退格,组合框应返回000-Test1并显示上述以及000-Test12,000-Test13等。

组合框文本编辑器文本更改事件中的逻辑已经从MSDN线程改编 - Implimenting AutoComplete组合框 - Yiling Lai,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;
            }
        }
    }

xaml:

    <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;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这可能会让你到那儿。我绑定了Text,它确实在退格时被调用。

Text="{Binding Path=CBvalue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

我做了类似的事情,但我继续下载超过20,000个值。我使用文本框和单独的列表视图。虚拟化对于所有20,000个并不是很重要。然后在文本更改后,我在使用DispatcherTimer之前等待1秒,然后进行过滤,以便在键入时不进行过滤。然后DispatchTimer调用BackGroundWorker来应用过滤器,这样我就可以取消BackGroundWorker。我只是将ListView绑定到使用LINQ过滤的List。往返DB的往返很慢。我的清单是半静态的。

相关问题