Alex Rivera | Logout

Prevent double-click from double firing a command

Asked 2009-05-08T19:06:49.837
20

Given that you have a control that fires a command:

<Button Command="New"/>

Is there a way to prevent the command from being fired twice if the user double clicks on the command?

EDIT: What is significant in this case is that I am using the Commanding model in WPF.

It appears that whenever the button is pressed, the command is executed. I do not see any way of preventing this besides disabling or hiding the button.

Edit
Report

1 Answer

5

You can use the EventToCommand class in the MVVMLightToolkit to prevent this.

Handle the Click event and send it through EventToCommand from your view to your viewmodel (you can use EventTrigger to do this).
Set MustToggleIsEnabled="True" in your view and implement a CanExecute() method in your viewmodel.
Set CanExecute() to return false when the command starts to execute and back to true when the command is done.

This will disable the button for the duration of processing the command.

answered 2011-03-22T13:55:14.500

Your Answer