9

In context of Microsoft's MVVM pattern and its Commanding/Event Handling aspects consider I am doing both binding a Command to a Control (say a Button) and subscribing to the control's Click event.

Is there any general rule what action takes place first - the processing of the code associated with the command or the one associated with the corresponding event handler?

marc wellman
  • 5,808
  • 5
  • 32
  • 59
  • I guess the biggest question, is why you would do both. The key thing about making use of commands is that you would normally bind this to an ICommand in a ViewModel, and by doing this, the VM does not need to know anything about the Button. Is this a case of mixing some code behind and a VM? If you are using a VM, how are you binding to the buttons event? – stevethethread Jul 12 '12 at 15:00
  • 1
    @SteveSolomon Well I am trying to deal with the dynamical creation of controls during runtime. And in order to respect the MVVM pattern I try to leave all the necessary code in the VIEW. So I am currently using command binding for the connection between VIEW/VIEWMODEL and the EventHandlers for 'Inter-VIEW' manipulation. – marc wellman Jul 12 '12 at 15:04
  • I don't think it's specific. I *think* off hand the first one to subscribe to the event would be raised first. If the command is hooked up in the XAML, I expect that would subscribe first. But, there's more than one way to skin a cat. – Peter Ritchie Jul 12 '12 at 16:05
  • 2
    If it isn't documented, the behavior may change in the future. I'd suggest programming like it may change from call to call. –  Jul 12 '12 at 19:46

1 Answers1

7

EventHandlers are triggered before the bound ICommand.

Jone Polvora
  • 2,184
  • 1
  • 22
  • 33
  • 2
    This is correct. I've occasionally done both before when I want some view-specific check to occur prior to processing the command. If the check fails, the event can cancel the command. – Rachel Jul 12 '12 at 15:49