【发布时间】:2018-10-17 16:32:44
【问题描述】:
假设我有一个名为comboBox 的ComboBox。
我想禁用ComboBox 的自动完成功能。
起初我以为我需要做的就是将其IsTextSearchEnabled 设置为false 如下
comboBox.IsTextSearchEnabled = false;
但这样做似乎会导致一些意想不到的副作用。
当IsTextSearchEnabled = true(默认为组合框)时,如果您尝试为ComboBox 的Text 设置值,组合框将在其ItemsSource 中找到相应的索引并相应地更新其SelectedIndex .
List<string> lst = new List<string>();
lst.Add("1");
lst.Add("2");
lst.Add("3");
lst.Add("4");
lst.Add("5");
MessageBox.Show(comboBox.SelectedIndex.ToString()); // -1
comboBox.ItemsSource = lst;
comboBox.Text = "3";
MessageBox.Show(comboBox.SelectedIndex.ToString()); // 2
现在,当我尝试设置 IsTextSearchEnabled = false 时,ComboBox 的 SelectedIndex 在其 Text 更改时不会更新。
List<string> lst = new List<string>();
lst.Add("1");
lst.Add("2");
lst.Add("3");
lst.Add("4");
lst.Add("5");
MessageBox.Show(comboBox.SelectedIndex.ToString()); // -1
comboBox.IsTextSearchEnabled = false;
comboBox.ItemsSource = lst;
comboBox.Text = "3";
MessageBox.Show(comboBox.SelectedIndex.ToString()); // -1
我想知道是否有办法同时实现这两者(即禁用自动完成功能,并且 ComboBox 在其文本更改时仍会自动更新其 SelectedIndex)
【问题讨论】:
标签: wpf combobox autocomplete