Question

I have a combobox that lists hard drives, binded to that for selected value is a string called currentdrive which when selected value on the box changes, enumerates selected hard drive, filling a listbox with image files, but I have a second listbox that needs to also know the current hard drive, this listbox won't be displaying images, I intend it to display metadata about the images but I need this box to know what the selected harddrive is before it can display metadata. Because metadata etc will be in its own class how can I access the currentdrive string without, repeating the currentdrive string, I want the metadata class to be able to share that functionality.

Was it helpful?

Solution

From what I understand, you have what is in essence, a second ViewModel, and you want to access data from the original ViewModel, the solution to that is what's called a Singleton Pattern, and is used in this fashion:

public class ViewModel()
{
    public static ViewModel Instance;

    public ViewModel()
    {
        Instance = this;
    }

    public string foo = "bar";
}

In your second class:

public class DifferentViewModel()
{
    public DifferentViewModel()
    {
        this.bar = ViewModel.Instance.foo;
    }

    public string bar;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top