如何知道ComboBox中何时“重新选择”某个值?

我正在使用ComboBox将文本模板插入到RichEdit控件中(模板的名称位于ComboBox的选项列表中。)

除非用户再次在列表中选择相同的值,否则一切都很有效。 然后SelectionChanged没有触发。 根据事件的名称(选择更改 ),这是有道理的,但我需要知道该值已重新选择,因此我可以再次插入它。

有没有办法知道从ComboBox中重新选择了一个项目? (或者更好的控制使用?)

我尝试使用DropDownClosed事件,但即使没有重新选择该项,也会触发。 (他们打开下拉菜单然后点击另一个控件。)

听起来你使用combobox的方式与正常使用不一致。 是否可以在插入所选模板的combobox旁边放置一个按钮。 我认为这种行为对于熟悉谷歌搜索搜索行为的用户来说会更好

我有同样的问题,我终于找到了答案:

您需要像这样处理SelectionChanged事件和DropDownClosed:

在XAML中:

  1 2 3  

在C#中:

 private bool handle = true; private void ComboBox_DropDownClosed(object sender, EventArgs e) { if(handle)Handle(); handle = true; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox cmb = sender as ComboBox; handle = !cmb.IsDropDownOpen; Handle(); } private void Handle() { switch (cmbSelect.SelectedItem.ToString().Split(new string[] { ": " }, StringSplitOptions.None).Last()) { case "1": //Handle for the first combobox break; case "2": //Handle for the second combobox break; case "3": //Handle for the third combobox break; } } 

我能找到的最好的方法是在下拉列表打开时清除所选值。 这并不理想,因为用户无法将其先前的位置保留为参考点(适用于长列表)。 但这是我能找到的最佳解决方案。

这是我使用的代码:

 ctor() { myComboBox.DropDownOpened += OnDropDownOpened; } private void OnDropDownOpened(object sender, EventArgs e) { var comboBox = ((ComboBox)sender); comboBox.SelectedItem = null; } 

我会用一个列表框。 用户无需打开它。 相反,他可以立即从列表中选择一个项目。 另外,我不会将此函数附加到SelectionChanged事件,而是附加到MouseDoubleClick事件。 这允许容易地选择和重新选择项目。 我还会添加一个按钮,它会触发相同的function。 双击应仅是此按钮的一种快捷方式,按钮也可以具有描述性文本和/或图标,这使其更加直观。

即使用户使用箭头键上下移动, SelectionChanged也会触发,但不应触发该function。

您需要提供一些代码来显示您要执行的操作。 如果已经选择了某个项目,为什么还没有设置RichEdit控件的模板呢?
在处理WPF时,它有助于熟悉绑定到视图模型,而不仅仅是像ItemsSource这样的东西,还有SelectedItem 。 根据您描述的场景,我将使用SelectedItem绑定到View Model,然后将RichEdit控件的模板绑定到相同的View Model属性,必要时使用值转换器。 这样您就不需要处理点击事件等。 如果View Model的属性是DependencyProperty或触发PropertyChanged事件(请参阅INotifyPropertyChanged ),则RichEdit控件的模板应自动反映下拉列表中的选择。

**编辑**根据您的评论,我假设您想要的行为是根据组合选择设置文本,但允许用户自定义该文本。 但是,如果已编辑,则应该能够重新选择组合值以重置文本。 麻烦的是,如果项目已经被选中,那么没有被触发的事件。 解决方案是,如果文本内容发生更改,则应取消选择任何组合选择(因为组合不再反映文本框的内容。)绑定可以非常好地管理:

此示例视图使用TextBox来简化:

          

ViewModel:

  class TemplateSampleViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public ObservableCollection Templates { get; private set; } private TextTemplate _selectedTemplate; public TextTemplate SelectedTemplate { get{ return _selectedTemplate; } set { if ( _selectedTemplate == value ) return; _selectedTemplate = value; if (_selectedTemplate != null) Text = _selectedTemplate.TemplateText; firePropertyChanged("SelectedTemplate"); } } private string _text; public string Text { get { return _text; } set { if ( _text == value ) return; _text = value; firePropertyChanged( "Text" ); var matchingTemplate = Templates.FirstOrDefault( t => t.TemplateText == _text ); SelectedTemplate = matchingTemplate; } } public TemplateSampleViewModel(IEnumerable templates) { Templates = new ObservableCollection(templates); } private void firePropertyChanged(string propertyName) { if ( PropertyChanged != null ) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 

接线:

  var viewModel = new TemplateSampleViewModel(new[] { new TextTemplate {Name = "First", TemplateText = "This is the first item"}, new TextTemplate {Name = "Second", TemplateText = "This is the second item"}, new TextTemplate {Name = "Third", TemplateText = "This is the third item"}, }); var test = new UITemplateSample {DataContext = viewModel}; test.Show(); 

这会绑定combobox,然后在选择项目时,文本框会自动更新。 当文本框内容改变时,检查模板以查看它是否仍然匹配,如果不匹配,则取消选择组合项。 如果条目与模板匹配,则自动选择该模板。