What is the difference between WPF Command and Event in WPF?
Asked Answered
T

2

8

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?

Tetter answered 17/4, 2013 at 11:58 Comment(0)
D
6

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.

Duad answered 17/4, 2013 at 12:5 Comment(0)
E
5

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.

Eviaevict answered 17/4, 2013 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.