Access properties in other ViewModels in MVVM Light

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; } ... 
+8
c # mvvm-light
source share
3 answers

In fact, you can use ViewModelLocator. By default, the inversion of the control container is used, so even if you create a new instance of Locator, you will get the same instance of single-mode models with the container.

Locator Class:

 static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<ViewModel1>(); SimpleIoc.Default.Register<ViewModel2>(); SimpleIoc.Default.Register<ViewModel3>(); } public ViewModel1 ViewModel1 { get { return ServiceLocator.Current.GetInstance<ViewModel1>(); } } public ViewModel2 ViewModel2 { get { return ServiceLocator.Current.GetInstance<ViewModel2>(); } } public ViewModel3 ViewModel3 { get { return ServiceLocator.Current.GetInstance<ViewModel3>(); } } 

then from the code you can access it as

 var vm1 = (new ViewModelLocator ()).ViewModel1; 

you get a single viewmodel instance.

from xaml: Resources (the default is Application.Resources in App.xaml)

 <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 

and a DataContext for the view (user controls or windows or something else)

 <UserControl ... DataContext="{Binding ViewModel1, Source={StaticResource Locator}}" ... > 
+17
source share

If you only need to associate the property with the main view model, and inside the content control, just use this syntax:

  ... Binding="{DataContext.mainvmpropertyname, ElementName=xxxx}" 

where xxxx is the name attached to the content control (or any control that has the main view model as its DataContext). Alternatively, you can use relative binding instead of the element name.

+1
source share

You can access the public properties of the ViewModel Locator using the Locator program from the Apps resources:

 MyViewModel vm = (App.Current.Resources["Locator"] as ViewModelLocator).MyViewModel 

or by creating another static instance in the ViewModelLocator class:

 public class ViewModelLocator { public static ViewModelLocator Instance = new ViewModelLocator(); static ViewModelLocator(){ ... } public MainViewModel Main { ... } } 

Related topics

+1
source share

All Articles