/// <summary> /// main logic to generate auto suggestion list. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> /// instance containing the event data.</param> void textBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender as TextBox; textBox.AutoWordSelection = false; // if the word in the textbox is selected, then don't change item collection if ((textBox.SelectionStart != 0 || textBox.Text.Length==0)) { this.Items.Clear(); //add new filtered items according the current TextBox input if (!string.IsNullOrEmpty(textBox.Text)) { foreach (string s in this.autoSuggestionList) { if (s.StartsWith(textBox.Text, StringComparison.InvariantCultureIgnoreCase)) { string unboldpart = s.Substring(textBox.Text.Length); string boldpart = s.Substring(0, textBox.Text.Length); //construct AutoCompleteEntry and add to the ComboBox AutoCompleteEntry entry = new AutoCompleteEntry(s, boldpart, unboldpart); this.Items.Add(entry); } } } } // open or close dropdown of the ComboBox according to whether there are items in the // fitlered result. this.IsDropDownOpen = this.HasItems; //avoid auto selection textBox.Focus(); textBox.SelectionStart = textBox.Text.Length; }