I have a main ViewModel containing a list of elements that I use in a certain amount of UserControls that are displayed in ContentControl on the main view. My current way of exchanging data between ViewModels exists is to make a link to each of the ViewModels in the main ViewModel and one of the main ViewModel in each UserControl . However, I don’t know, because this is the right way to do this, since I have a ViewModelLocator , and I kind of expect this class to have some functionality to support something like this.
Can someone tell me that I am doing this normally, or if there is a better way to do this in MVVM Light?
EDIT:
I found this when I was looking for something else, was this the best solution?
private ViewModelLocator locator = new ViewModelLocator();
And then using locator properties to reference each ViewModel?
EDIT2:
Apparently, what I thought would work, no, at first I only had links in the main ViewModel , and it worked, but when I try to do this in UserControls , it crashes my application. I am currently trying to find a possible solution for the first editing.
MainViewModel.cs (others are similar, referring only to the main ViewModel)
public class MainViewModel : ViewModelBase { private ItemAddViewModel itemAddViewModel; private ItemsViewModel itemsViewModel; /// <summary> /// Initializes a new instance of the MainViewModel class. /// </summary> public MainViewModel() { ItemsList = Item.GetItemsList(); itemAddViewModel = ServiceLocator.Current.GetInstance<ItemAddViewModel>(); itemsViewModel = ServiceLocator.Current.GetInstance<ItemsViewModel>(); ShowItemsView(); } ... private void ShowItemsView() { CurrentControl = itemsViewModel; } ...
c # mvvm-light
Kryptoxx
source share