-1

I'm trying to learn MVVM Pattern doing a simple GUI using WPF . It's just a simple plugin for Autocad with a modal window showing some information about the model.

I have two Usercontrols with their respective views and viewmodels. I'm showing these usercontrols in a single windows/dialog. One of these usercontrols needs to show some information, representing a simple List (the model) in a DataGrid.

Like I said before, the model for that user control is a existing List. I need to pass the List information to the viewmodel but I'm struggling with this. I was thinking on passing it as a parameter to the MainWindow contructor and then to the respective viewmodel but doesn't sound like a good idea.

What options do I have in this case?

I'm using MVVM Toolkit.

Thanks!

Eduardo
  • 687
  • 1
  • 6
  • 24

1 Answers1

1

You could use the WeakReferenceMessenger/StrongReferenceMessenger to send a message from one view model to another:

// Create a message
public class LoggedInUserChangedMessage : ValueChangedMessage<User>
{
    public LoggedInUserChangedMessage(User user) : base(user)
    {
    }
}

// Register a message in some module
WeakReferenceMessenger.Default.Register<LoggedInUserChangedMessage>(this, (r, m) =>
{
    // Handle the message here, with r being the recipient and m being the
    // input message. Using the recipient passed as input makes it so that
    // the lambda expression doesn't capture "this", improving performance.
});

// Send a message from some other module
WeakReferenceMessenger.Default.Send(new LoggedInUserChangedMessage(user));

Please refer to the docs for more information.

mm8
  • 163,881
  • 10
  • 57
  • 88