Note that there are some explanatory texts on larger screens.

plurals
  1. POComboBox with progressive auto-complete
    primarykey
    data
    text
    <p>We need to populate a combobox with data retrieved from a database. Because the number of potential records retrieved could be in the thousands, we limit the list by not even calling the database until the user has entered the first 5 characters in the combobox. Then the list is populated and progressively filtered as the user keys in additional characters using combobox.Items.Filter.</p> <p>The problem is that the process is not reversible. Let's say the user has entered 000-Test11 and what is being shown is 000-Test11 and 000-Test111. If the user hits backspace, the combobox should go back to 000-Test1 and show the above in addition to 000-Test12, 000-Test13, etc. </p> <p>The logic in the combobox text editor text changed event has been adapted from an MSDN thread - Implimenting AutoComplete combobox - Yiling Lai, Rovi Corporation answer: <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/</a>.</p> <p>Here is the code to demonstrate the issue we are having. </p> <p>StudyProxy class:</p> <pre><code> 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; } } } </code></pre> <p>The xaml:</p> <pre><code> &lt;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"&gt; &lt;Grid&gt; &lt;DockPanel Height="250" Width="250"&gt; &lt;ComboBox DockPanel.Dock="Top" VerticalAlignment="Top" Name="comboBox1" IsEditable="True" IsReadOnly="False" Margin="10" DisplayMemberPath="StudyNumber" SelectedValuePath="StudyID" SelectionChanged="comboBox1_SelectionChanged"/&gt; &lt;DataGrid Name="dataGrid1" /&gt; &lt;/DockPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>The code behind:</p> <pre><code> 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 { /// &lt;summary&gt; /// Interaction logic for SelectStudyScreen.xaml /// &lt;/summary&gt; 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&lt;string&gt; studyIDs = new List&lt;string&gt;(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 &lt; 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 =&gt; { if ((a as StudyProxy).StudyNumber.StartsWith(text, StringComparison.OrdinalIgnoreCase)) { return true; } return false; }; _comboBox1Popup.IsOpen = true; } } private List&lt;StudyProxy&gt; GetTestData() { List&lt;StudyProxy&gt; list = new List&lt;StudyProxy&gt;(); 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload