我怎么能知道,当值是QUOT;再选择"在组合框?组合、再选、我怎么能、QUOT

2023-09-05 00:19:12 作者:‵莫念

我使用的是组合框插入一个文本模板到RichEdit控件(模板的名称是选择列表的组合框。)

I am using a ComboBox to insert a text template into a RichEdit control (the name of the template is in the picklist for the ComboBox.)

这一切的伟大工程,当用户再次选择列表中的相同值的除外。那么的SelectionChanged 不点火。这使得基于事件的名称意义(选择的更改的),但我需要知道的价值被重新选择,所以我可以重新插入。

It all works great except when the user selects the same value in the list again. Then the SelectionChanged is not firing. That makes sense based on the name of the event (SelectionChanged), but I need to know that the value was re-selected so I can insert it again.

有没有办法知道,一个项目从组合框重新选择?(或更好的控制使用?)

Is there a way to know that an item was re-selected from the ComboBox? (Or a better control to use?)

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

I tried using the DropDownClosed event, but that fires even if the item was not re-selected. (They open the drop down then click on another control.)

推荐答案

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

I had the same question and I finally found the answer:

您需要同时处理SelectionChanged事件和DropDownClosed是这样的:

You need to handle BOTH the SelectionChanged event and the DropDownClosed like this:

在XAML:

<ComboBox Name="cmbSelect" SelectionChanged="ComboBox_SelectionChanged" DropDownClosed="ComboBox_DropDownClosed">
  <ComboBoxItem>1</ComboBoxItem>
  <ComboBoxItem>2</ComboBoxItem>
  <ComboBoxItem>3</ComboBoxItem>
</ComboBox>

在C#:

In 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;
  }
}