What is the difference between WPF Command
and Event
?
5 Answers
Generally speaking you can do almost the same with events as with commands, it is just a different pattern of handling user interaction.
Commands
in WPF allow you to move the implementation of a command handler to the buisness layer. Commands combine Enable state and executation, so everything is in place. Reade more by searching for the MVVM pattern.
Commands are more complex to implement at first, so if your application is small you should consider just sticking to events.
-
Command is commonly used pattern in SOA and events are in Event driven architecture (EDA). – Liton Sep 20 '10 at 09:00
-
2"Commands in WPF allow you to move the implementation of a command handler to the buisness layer." this is not true. It must be "outside the view". – Aliostad Sep 20 '10 at 09:22
-
6@Aliostad IMHO, a "business layer" is in general "outside the view". – Liton Sep 20 '10 at 09:58
-
I won't agree to that; Commands is a very powerful and useful feature of WPF and provides a lot of advantages over events. If you are considering WPF seriously then you must start using commands ASAP. It really simplifies the development and is really easy to use(you will have to spend some time though). – akjoshi Sep 20 '10 at 12:09
-
1@akjoshi I didn't say anything different. For small projects I just wouldn't use commands because of their overhead. – testalino Sep 20 '10 at 14:11
Commands
are similar to Events
except we can associate any number of UI Controls or Input Gestures to a command and bind that command to a handler that is executed when control are activated or gestures are performed.
Command
also keep track weather or not they are available. If they are not available then all controls associated with that command are disabled.
The Code that executes when command is invoked is located in commands Execute
event handler.
The Code that determines is command can be or can not be invoked is located in commands CanExecute
event handler.
WPF have some inbuilt Commands:
Command Class | Example Commands
-----------------------------------------------
ApplicationCommands | Close, Cut, Copy, Paste, Save, Print
NavigationCommands | BrowseForward, BrowseBack, Zoom, Search
EditingCommands | AlignXXX, MoveXXX, SelectXXX
MediaCommands | Play, Pause, NextTrack, IncreaseVolume, Record, Stop
You can bind WPF command in the view (XAML) and receive the event raised. This way you do not have to use code behind which is a no-no in MVVM.
So the binding element is very important. But it also implements CanExecute
which normally makes your control disabled if it returns false, e.g. if it is a button.

- 80,612
- 21
- 160
- 208
-
Your feedback is appreciated more if you include more detail and more referneces if possible. For a beginner your feedback has referneces that expereices WPF developers can follow. Thank you – user1298925 Nov 19 '13 at 17:14
In events an action is tightly coupled with its source and can't be reused freely; Using commands you can easily maintain various actions in a single place and reuse them anywhere in application.
What makes commands different from a simple event handler attached to a button or a timer is that commands separate the semantics and the originator of an action from its logic. This allows for multiple and disparate sources to invoke the same command logic, and it allows the command logic to be customized for different targets.
taken from - Commanding Overview: http://msdn.microsoft.com/en-us/library/ms752308(v=VS.90).aspx
This article explains the concept of Commands and is must read before using commands.
This SO thread is also havign a lot of useful info. :
Roughly speaking, Command
is encapsulation of Object
(Button, Menu) Enable/Disable State and Action.
Limitation of Command
:
- Multicast (you can use multi binding but not so common and don't feel as nice as event multicast)
- Need 2 step to light up the bulb. (If your button is always enable, then you just waste your code).