Passing parameter from one viewmodel to another one using messenger

mvvm-light, wpf

Solution

Your syntax would look something like this:

//Subscribe
Messenger.Default.Register<OpewNewWindowMessage>(OpenNewWindowMethod);

// Broadcast
var message = new OpewNewWindowMessage();
message.ViewModel = this;
Messenger.Default.Send<OpewNewWindowMessage>(message);

// Subscribed method would look like this
void OpenNewWindowMethod(OpewNewWindowMessage e)
{
    // e.ViewModel would contain your ViewModel object
}

In the above example, you would create a new class called `OpewNewWindowMessage` and give it a property of `ViewModel`, then you would populate that value before broadcasting the message.

The `OpenNewWindowMethod()` would receive the message, and could access `OpewNewWindowMessage.ViewModel` to access the ViewModel property

Technically you don't need to create a message object if you're only passing around one property, however I usually find it makes the code easier to read and maintain if you create a message object instead of using a generic `<object>` like you have in your code.

Problem

Whenever I want to open a new window from a View model, normally I am using messenger . But now I want to open a new window from a view model and pass an object from calling view model to called view model . How can I implement this? In my viewmodelbase class currently I am having following methods. ``` public void SendNotificationMessage(string notification) { Messenger.Default.Send<NotificationMessage>(new NotificationMessage(notification)); } public void SendNotificationMessageAction(string notification, Action<object> callback) { var message = new NotificationMessageAction<object>(notification, callback); Messenger.Default.Send(message); } ``` Please help me

Original source

Related problems