Accessing a property in one ViewModel from another
Asked Answered
Q

1

14

I want main viewmodel to have a certain list, and then access from many other viewmodels.

For example, in MainViewModel.cs I will have a list of 50 numbers, then in NumListViewModel.cs, I'd like to access it in order to show it as a list, and in AddNumViewModel.cs I'd like to be able to update that list.

It's been suggested that I use events / evenaggerator, which I did, but unfortunately, for all I know all I can do with it is send a num from one view to another and tell it to update the list, but the problem is, as the program grows, I will need to have a lot of subscribers in the main view model, and when something actually happens I will have to "publish" events according to the number of subscribers which makes it even harder to maintain.

I also found another answer, instructing to create an instance of anotherVM within the mainVM, with a parameter set to "this" which is a reference to the mainVM. It works, but then again, it could get quite long.

So my question is, is there a better way to access a property from another VM?
Like literally have the an instance of the class that holds the list in the mainVM, and then just be able to update / access it from the other VMs, without having to explicitly program which VM can. Would make life so much easier.

In your answer, please try to avoid suggesting frameworks. Although there are some really good ones, I want to be able to do at least that by myself.

For example:

MainVM.cs:

public class MainVM
{
    List lst = new List(); //Let's just say it's full...
}

OtherVM.cs:

public class OtherVM
{
    lst.Add(3);
}

PS: Yes I know it has been asked already, and yes I have done my research, BUT I the answers I found are too 'static', I guess?

Queensland answered 12/5, 2013 at 11:0 Comment(1)
Use Prism to resolve instance of your MainViewModel.Swaggering
S
25

If you want direct access to the list from an external ViewModel, then your options are to:

  1. Pass the List to the OtherVM as a constructor argument or public property. Then the OtherVM can treat it like a member.

  2. Pass the MainVM to the OtherVM as a constructor argument or public property. Then the OtherVM can access the List by first accessing the MainVM.

Example:

public class MainVM
{
    public List<XX> MyList { get; set; }
}

public class OtherVM
{
    public MainVM TheMainVM { get; set; }

    public OtherVM(MainVM theMainVM)
    {
        TheMainVM = theMainVM;
        
        // Access the MainVM's list 
        TheMainVM.MyList.Add(stuff);            
    }
}
  1. Give the MainVM a static property called "Default" or "Instance," so you can access the static instance of MainVM from within OtherVM, without assigning it as a member field.

Example:

public class MainVM
{
    private static MainVM _instance = new MainVM();
    public static MainVM Instance { get { return _instance; } }

    public List<XX> MyList { get; set; }
    //other stuff here
}

//From within OtherVM:
MainVM.Instance.MyList.Add(stuff);
Schnabel answered 12/5, 2013 at 11:56 Comment(6)
Thank you so much! Option #3 did the job! Options 1 and 2 work fine, but as the project grows, I will have to pass the instance/list more than once, so I guess this is not a good practice for large-scale applications.Queensland
@BTownTKD I know this question is somewhat old but I have a problem. I have tried your code but in the reverse order. i.e. I have declared Instance property on OtherVM and I want to access it in mainVM. I dont get any errors but when I access any property of OtherVM in MainVM I dont get the value of that property. I mean I get null instead of that property's value.Gendarmerie
You'll want to post that as a new question.Schnabel
@BTownTKD could you post an example of the approach in #2? How exactly do I pass the list/object as a ctor argument?Turpitude
@BTownTKD I have a problem with the 3 rd option in the following scenario: In the constructor of MainVM I am calling OtherVM constructor. In OtherVm constructor, I have used the Instance property. In this case Instance is null.Jiggle
@Jiggle this sounds like it should be a new question, about resolving order of initialization between two inter-dependent/coupled objects.Schnabel

© 2022 - 2024 — McMap. All rights reserved.