Using the following simple text box as an example:
Angus/ComboBoxItem
This solution is based on user1234567's answer, with a couple changes. Instead of searching the Items list it simply checks the ComboBox's SelectedIndex for a value >= 0 to see if a match was found and it resolves RB's concern about holding down a key inserting more than one character. It also adds an audible feedback when it rejects characters.
private int _lastMatchLength = 0;
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
_lastMatchLength = 0;
}
private void myComboBox_KeyUp(object sender, KeyEventArgs e)
{
ComboBox cBox = sender as ComboBox;
TextBox tb = cBox.Template.FindName("PART_EditableTextBox", cBox) as TextBox;
if (tb != null)
{
if (cBox.SelectedIndex >= 0)
{
_lastMatchLength = tb.SelectionStart;
}
else if (tb.Text.Length == 0)
{
_lastMatchLength = 0;
}
else
{
System.Media.SystemSounds.Beep.Play();
tb.Text = tb.Text.Substring(0, _lastMatchLength);
tb.CaretIndex = tb.Text.Length;
}
}
}