简体   繁体   中英

WPF How to cancel user's selection in combobox?

There is a combobox with some items.

If user wants to select the other item, I want to show a Yes-No question box to confirm.

If the user clicks on 'No', then change will be canceled.

I do this in "SelectionChanged" event handle, but if I cancel the change and select the old item, this event fires again and agin...

  • How to avoid it?
  • How to ask yes/no before changing the selection?

If you do not care about displaying the value to change to before really accepting the change (by clicking "Yes") you could try to use the SelectionChangeCommitted event handler to handle these changes and display the user dialog.

If you want to ask user after user trying to change that use this event handler SelectionChangeCommitted . You can also change the state of Combo box to initial form by simply set the index number of combobox to -1

 comboBox1.SelectedIndex = -1;

so you can write the code above in click event handler of "No" button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM