我怎么能知道,当值是QUOT;再选择"在组合框? [英] How can I know when a value is "re-selected" in a ComboBox?

查看:87
本文介绍了我怎么能知道,当值是QUOT;再选择"在组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是组合框插入一个文本模板到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#:<​​/ P>

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

这篇关于我怎么能知道,当值是QUOT;再选择&QUOT;在组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆