Updating a viewmodel from another viewmodel?

There are any number of ways to do this. Pass a reference to the main/parent view model into the child and have the child call the main view model. Have the child view model fire an event that the parent subscribes to.

Use a messenger/mediator to communicate between the two. The parent subscribes, the child posts the message. This provides loose coupling.

Set the main view model up as a global service. Register it somehow. Have the child look up the service (requiring global services is a pretty common problem) and then call something on the global/common interface.

From my experience, the simplest method would be #2. Define an event on the child view model. The parent will have to look up the child (I don't know if it contains it or how your view models are constructed) and subscribe to the event.

Any of these methods could be appropriate. It depends a lot on the context. Coupling is not appropriate unless the VMs are closely and conceptually related.

– Josh G Apr 20 at 15:30.

Best way to do it is to have some kind of reference from child to parent and to update this parent when closing the child. Or you can have some kind of event on child and let parent listen on this event. You then raise this event when child closes.

Messaging is used when both ViewModels are logicaly unrelated. Rant Don't people even know basic principles of OOP or what? /rant.

I don't think using a messenger in this case is necessarily bad design. It would depend on any number of factors, but sending a simple message might be a lot simpler and more effective than setting up an OO relationship between the two VMs – Adam Rackis Apr 20 at 15:50 I think the issue is that there isn't a best practice method of doing this as part of the MVVM pattern. – Will?

Apr 20 at 15:51.

The standard way to communicate between ViewModels is to use Messaging of some sort. One good implementation of this is the MVVM Light Toolkit Here's some (random) code using the default messenger therefrom: //Registering: Messenger.Default. Register>(this, true, fillSourceWith); Messenger.Default.

Register(this, ChangeMainTemplates); //with a specific "token" Messenger.Default. Register(this, MessageTokens. ClearList, o => BookSource.Clear()); //Sending Messenger.Default.

Send>(newBooks); Messenger.Default. Send(DisplayModeEnum. MediumLayout); Messenger.Default.

Send(null, MessageTokens. ClearList).

1. He is talking about parent/child relationship. Messaging is not good idea here.

– Euphoric Apr 20 at 15:31 I think messaging should be fine here. Without messaging you'd having to couple your view models somehow. – Adam Rackis Apr 20 at 15:33 Messaging may or may not be URL1 may be too complicated for this problem, but it is a good solution.

– Josh G Apr 20 at 15:34 @Josh G - I agree. There are other ways that might work equally well based on his situation. +1 on your answer for showing others – Adam Rackis Apr 20 at 15:36 He asked for the "best" solution, but "best" is very subjective and requires more information about his context.

– Josh G Apr 20 at 15:39.

I had the same problem a few days before ;-) Finally I used a mediator to communicate both view-models. On fact I used the Messenger from MVVM Light. Public void Search(object parameter) { ChildWindow window = new ChildWindow(); SearchWindow pageSearch = new SearchWindow(); window.

Content = pageSearch; window.Show(); Messenger.Default. Register(this, action => this. CloseWindow(action)); } After that I defined the Message with everything I needed to know from main window: public class CloseWindowMessage : MessageBase { public bool Result { get; set; } public Model.

Selected Selected { get; set; } } Finally the message back from the childwindow you only have to register the message with result and the object you want to get back. You should register from the code-behind of your view to close the window.

I think best way to pass the message between two view models is event programming.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions