Updating a viewmodel from another viewmodel

青春壹個敷衍的年華 提交于 2019-11-30 19:33:29

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.

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<IEnumerable<BookViewModel>>(this, true, fillSourceWith);
Messenger.Default.Register<DisplayModeEnum>(this, ChangeMainTemplates);
//with a specific "token"
Messenger.Default.Register<object>(this, MessageTokens.ClearList, o => BookSource.Clear()); 

//Sending
Messenger.Default.Send<List<BookViewModel>>(newBooks);
Messenger.Default.Send<DisplayModeEnum>(DisplayModeEnum.MediumLayout);
Messenger.Default.Send<object>(null, MessageTokens.ClearList);

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] Dont people even know basic principles of OOP or what?? [/rant]

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<Messages.CloseWindowMessage>(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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!