0

My project is build on MVVM. Currently I have a list where i can select an object and add them to another list. What I want to make is a new window where this list is shown (the list with objects that are added) and edit that list in the new window (delte an item from that list).

How should I pass the data (selected object) to another window and be able to update them there?

I currently have it working in one view. In some related questions they advice MVVM light so I tried looking for that, from what I red mvvm light is mostly used to replace the notify property change. Should I use mvvm light or are there some specific patterns I could use?

Both windows will be open at the same time.

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

2 Answers2

1

If you want to share your ViewModel between windows, you can use a ViewModelLocator. It is not specific to MvvmLight, it just creates one for you with its project template. You can implement it yourself, it is basically a container for your ViewModels. You can look here for the implementation details.

Community
  • 1
  • 1
Yusuf Tarık Günaydın
  • 3,016
  • 2
  • 27
  • 41
1

I've got to say that I'm not sure that these are the best approaches and if they are common, it's just what me and my colleagues were using in a WinRT application, so I'll be really glad if someone comes up with something better (both of these are not that clean).

I can think of two ways to pass data (without persisting it)

  1. Pass parameters on page navigation

  2. Have common shared class (Static or singleton class with some common data accessible from all ViewModels)

For passing on navigation:

I have this method in my Navigation service class:

public virtual void NavigateTo(Type sourcePageType, object parameter)
{
    ((Frame)Window.Current.Content).Navigate(sourcePageType, parameter);
}

and I use it like this in navigation commands:

Navigation.NavigateTo(typeof(PageType), someParameters);

Then you could get the values in the code behind of the navigated page

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var receivedParameter = e.Parameter as TheTypeOfThePassedParameter;
}

And from there to pass them to the ViewModel, maybe there is an option to pass this without code in the code behind but I've not tried this.

Having shared class:

This is pretty much straightforward just have static class or a singleton with the desired fields.

kirotab
  • 1,296
  • 1
  • 11
  • 18