52

Can anyone tell me what are the advantages of using Commands vs Events in WPF. Do Commands or Events run into memory leaks? What is the fastest approach. What are their disadvantages.

5 Answers 5

41

Commands provide two main benefits over event handlers:

  1. commands are not linked to the caller, so same command is not dependent and can be called from menu item, toolbar button, keyboard, etc.
  2. commands provide support for enabling/disabling all related UI controls based on the status of command (can be executed or not)

I'd prefer using commands at real project, especially if you want to use M-V-VM.

I haven't heard about any memory leaks related with commands.

Events are probably faster, but the difference should not be significant - I've been using commands on my projects for 2 years and hadn't any performance issues with them.

For more details on commands see Commanding Overview(archive)(v4)

2
  • 3
    Can't you subscribe the same event handler to a toolbar button, keyboard, etc? These seem like weak reasons for adding a whole layer of complexity to your code tbh. Jan 4, 2017 at 4:05
  • 8
    A better argument would be that event handlers can be managed only in code behind, you can’t simply move it to a ViewModel. Commands offer loose coupling. Jan 12, 2017 at 23:21
37

But although Commands and Events can be overlapping, they are two different things. Commands say "do this!", while events say "this just happened!". So you might have a CloseWindowCommand for closing a window, but the window might have a ClosingEvent that tells subscribing objects that is is closing.

2
  • @Shoe - What else did you expect? This totally makes sense and is not a contrary to what T.J.Kjaer said.
    – ˈvɔlə
    May 19, 2015 at 14:40
  • 7
    @Wolle The English of saying "this just happened" before "do this!" is what I was remarking about. I never said he was wrong. May 19, 2015 at 16:34
5

Commands are a more standard way to integrate events. the can be more usefull than events because with the help of them you can define a single task (command) and use it from different places. for example you can define a save command and use a menu item, a context menu item and a button to use it at the same time. this way you can centerlize the tasks. also commands support data-binding which is a very powerfull feature of WPF application. as far as I know, commands lead to certain kinds of memory leaks but you can avoid that by using many work arounds. I must add that MVVM design pattern also uses commands as a standard way to design WPF application. working with events is much simpler but commands provide much powerfull design. but you must now that you can't always use commands instead of events. there any many places that you can only use events.

0

Additionally WPF4.0 allows binding to command definitions. This makes it even easier to expose commands from your view models, utlimately helping you in separating logic from UI concerns.

1
-6

Commands are routed events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.