Autocomplete combobox for WPF

后端 未结 7 1170
小蘑菇
小蘑菇 2020-12-09 00:05

I need an autocomplete combobox for WPF C#. I\'ve tried several approaches but nothing works. For example I\'ve tried a combobox:



        
7条回答
  •  情歌与酒
    2020-12-09 00:26

    In XAML you should set IsEditable=True and add handler for PreviewKeyDown event:

    private void ComboBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var cmb = (ComboBox)sender;
            cmb.IsDropDownOpen = true;
            var textbox = cmb.Template.FindName("PART_EditableTextBox", cmb) as TextBox;
            cmb.ItemsSource = CurrentStorage.Organisations.Where(p => string.IsNullOrEmpty(cmb.Text) || p.Name.ToLower().Contains(textbox.Text.ToLower())).ToList();
        }
    

提交回复
热议问题