what is the difference between wpf command and event?
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.
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.
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).
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. :
http://stackoverflow.com/questions/8452/i-dont-grok-the-wpf-command-pattern