What event handler to use for ComboBox Item Selected (Selected Item not necessarily changed)

后端 未结 12 1579
囚心锁ツ
囚心锁ツ 2020-12-13 08:48

Goal: issue an event when items in a combobox drop down list is selected.

Problem: Using \"SelectionChanged\", however, if the user

12条回答
  •  心在旅途
    2020-12-13 09:12

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

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

    In XAML:

    
        1
        2
        3
    
    

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

提交回复
热议问题