Disable mouse clicks during long-running process?
I have an issue where when a button is clicked and the process takes a while, the user clicks on other locations on the GUI screen. Those button clicks get queued up in the dispatcher so after the initial button click those other locations get clicked. I want to prevent this from happening so other UI controls don't get clicked after it's done processing.
The only solution I can think of is to p/invoke into the mouse events and prevent them from being passed to the application while the button click is being processed. Is this the be开发者_运维技巧st approach?
One of the easier solutions to this is to have some sort of semi-transparent overlay (potentially with progress information) that you pop up when there are long-running tasks going on. That will reliably disable the entire UI until you remove the overlay (which keeps you from having to do a lot of changing/binding IsEnabled properties).
If the process takes a while, it is generally recommended to perform such process in a worker thread, rather than in a GUI thread. So use BackgroundWorker
approach like in this CodeProject article, and then use CanExecute
method of commands associated with specified buttons, like here:
WPF Commanding – When do Commands re-evaluate their CanExecute method?
WPF – CanExecute refreshed
EDIT:
You can bind IsEnabled
property of your other controls to the result of Command's CanExecute method.
How to disable combobox when command canExecute returns false
How to bind a ComboBoxItem's IsEnabled property to the result of a Command's CanExecute method
精彩评论