Search code examples
c#wpfdrop-down-menucomboboxbackspace

How to prevent user from deleting item by pressing "backspace"?


Users will usually expand the ComboBox, pick the desired option and the ComboBox will hide other options. Now user can delete the chosen option by pressing backspace button. May I know how to prevent it?


Solution

  • This could be avoided by handling the PreviewKeyDown event and marking any use of the backspace key as handled

    void OnComboPreviewKeyDown(object sender, KeyEventArgs e) { 
      if (e.Key == Key.Back) { 
        e.Handled = true;
      }
    }