This project has moved and is read-only. For the latest updates, please go here.

Call relay command from code

Nov 11, 2010 at 6:31 AM

Hi, Is it possible to call a relaycommand from code behind in a view?

Since a view model is binding to the view, I would like to know a way to get instance of the viewmodel's command,

then call it from code.

This would be a simpler solution for confirmation dialog in view. Since view can handle this confirmation dialog by itself.

Or another way is to binding the same view model to multiple view.

So a view will bind its view model to the dialog view, and dialog view can trigger the command.

I know the Messaging function in MVVMLight, I am just thinking about any other solutions.

Thanks a lot.


Nov 11, 2010 at 3:12 PM

Does the confirmation dialog need logic? if yes then use the messenger class.  If the dialog does something really simple that is only relate to ui then put it in code behind.

Nov 11, 2010 at 5:25 PM


Yes you can get and execute a RelayCommand as follows:

var vm = (MainViewModel)DataContext;




Nov 26, 2010 at 11:15 AM

Thanks, I am ending up to using message.

Messaging is a very good solution because it can communicate from view to VM or from VM to view.

but it can not target a message to specific instance.

For example, I have a 3 instances of a User Control in same window.

This user control will accept a dialog message. Then i will have 3 pop ups...

I haven't had a look at Service, so I am not sure if it can fix this issue.