WPF SelectionChanged to same value

前端 未结 6 2062
执念已碎
执念已碎 2021-01-05 22:46

I am using SelectionChanged in ComboBox item, but when I select the same item again the SelectionChanged function does not fire and I need it to do so. How can I tell it to

6条回答
  •  醉话见心
    2021-01-05 23:19

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

提交回复
热议问题