Question

I was just googling the difference between WPF Command and Event in WPF. i landed on the following page of stackoverflow where the discussion is going on.

What is the difference between WPF Command and Event?

I am only able to understand following from there

  1. Commands can be written in business layer while event only in presentation
  2. A single command can be associated with many controls but event can only be associated with only one control.

Am I right? Is there any other difference between them?

Was it helpful?

Solution

You're right but only partly.

  1. The main concern there is that events doesn't fit well into MVVM paradigm. Simply saying events are hardly pluggable, you can't bind to event handler. Nevertheless there is no limit to use commands in presentation layer but there is no benefits of doing so. As well as you could catch your control in BLL and attach event handler to it but this case is even worse.
  2. No. Event handlers also could be associated to different controls as long as event args type match.

Also commands gives you some free benefits. For example using command element'll be disabled if CanExecute() returns false. Another benefit is that using commands forces you to fo follow Separation of concerns principle.

OTHER TIPS

An Event is a trigger that occurs when something happens in the UI. A Command is how this event is handled by your domain model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top