zapico
zapico

Reputation: 2396

Avoid second click in silverlight button MVVM

I've seen I have a problem with several users that use to double-click in buttons.

I have several buttons bound to commands that launch many actions.

For example there are two windows that communicate between them through a mediator so when I click "close the other window", the bound command sends a "CloseTheOtherWindowMessage". The problem is that when a user makes double click it tries to close the window a second time and, as expected, it crashes.

I've tried to set the window BusyIndicator as IsBusy when I press the button but my finger is quicker than MVVM and it still let me double-click before it starts showing the BusyIndicator.

I've found many examples of how to only admit double click in MVVM using interaction.Behaviors but I want just the opposite. Is there any example or other good and general solution for this problem?

Upvotes: 1

Views: 681

Answers (1)

herzmeister
herzmeister

Reputation: 11297

  1. Why is it "as expected" when it crashes? A crash should never be "as expected".

  2. Your finger shouldn't be "quicker than MVVM". The Dispatcher thread always acts deterministically and sequentially. Do you use a multi-threaded approach?

  3. In the command's Execute method or handler, raise its CanExecuteChanged event, and the binding engine will immediately call CanExecute(...). Make it so that this method will return false the second time. Maybe use a timer, or, better yet, you can logically determine by your view model state alone that the action is not possible right now (i.e. because IsOtherStuffAvailable is currently false).

Upvotes: 1

Related Questions