Command in a separate file

Topics: Prism v4 - WPF 4
Dec 9, 2010 at 8:20 PM


i wanted to put my command in a separate file, but this seems to be harder than expected. When i write my EditContactCommand and i inherit from DelegateCommand, i get an compiler error. it sais that i have to use the constructor that gets the delegates of the methods. but its not possible to use the methods in my command class for the constructor of the base class. Do i always have to put my commands in my viewmodel???

Dec 10, 2010 at 12:27 PM


In order to create a Delegate command, you need to "instantiate a DelegateCommand field in the constructor of your view model, and then expose it as an ICommand property", according to the Commanding section of this chapter from the Prism MSDN documentation.

Take into account that, when using Delegate Commands, you don't need to write a new class for each command that you are implementing. You just need to create a new instance of DelegateCommand and pass different delegates to the instance. As for placing the commands in the View Model, you don't necessarily have to put it there for the Delegate Command to work, but that's the recommended scenario. What you do need to have is the possibility to bind to that command from your view. Generally, the View Model is the view's DataContext, so that's why it's recommended to place the Delegate Command in your View Model.

You might find the Commanding QuickStart useful to learn more about DelegateCommands.

I hope you find this helpful.

Guido Leandro Maliandi

Dec 11, 2010 at 12:47 PM

Well, thanks for answering, but i cannot find any separate command in this sample. And the documentation also creates commands always in the viewmodel.

The solution ist just do create a separate class, implement the ICommand interface raise the CanExecuteChanged event to refresh al bound GUI-Parts...