Autocomplete for ComboBox in WPF anywhere in text (not just beginning)

前端 未结 5 1431
挽巷
挽巷 2020-12-13 18:39

I\'ve got a ComboBox in WPF that I\'ve mucked around with quite a lot (it has a custom template and a custom item template). I\'ve got it to the point now where it is workin

相关标签:
5条回答
  • 2020-12-13 19:24

    WPF Combo box don't support Autocomplete

    Here is a sample that allows you to do this in an indirect manner, by applying a filter to the items.

    See http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cec1b222-2849-4a54-bcf2-03041efcf304/

    0 讨论(0)
  • 2020-12-13 19:29

    As far as I know there's no way to force standard ComboBox to behave this way by just changing a setting. So you'll have to implement your own combo box derivative for that or search for ready made 3rd party control (I believe there are plenty of them).

    0 讨论(0)
  • 2020-12-13 19:34

    Check out the following article in CodeProject: A Reusable WPF Autocomplete TextBox

    0 讨论(0)
  • 2020-12-13 19:34

    You could try handling the ComboBox's TextInput or PreviewTextInput events, doing the text search yourself, selecting the most appropriate item, and setting "e.Handled = true." Just a thought. Hope this helps!

    edit:

    This works for a single character (i.e. if you enter the letter "j", it will select the first item that contains a "j" or "J"), but I'm sure there's a way to do this with your control. Enjoy!

    private void MyComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e) {
        foreach (ComboBoxItem i in MyComboBox.Items) {
            if (i.Content.ToString().ToUpper().Contains(e.Text.ToUpper())) {
                MyComboBox.SelectedItem = i;
                break;
            }
        }
        e.Handled = true;
    }
    
    0 讨论(0)
  • 2020-12-13 19:42

    The combobox now supports autocomplete, just make sure in the xaml for the combobox put

    IsEditable="True"
    
    0 讨论(0)
提交回复
热议问题