lundi 14 juin 2021

What is the best way to use a static collection instance in another ViewModel?

I want to use a data collection declared in a singleton class in another ViewModel. However, each ViewModel must additionally display its own information. For example,

public static List<int> GlobalDataCollection { get; set; } = new List<int>();

This is a property of the singleton class. In other ViewModels, use it like this:

public class CustomType
{
   public int Number { get; set; } // Data for ViewModel only.
   public int GlobalData { get; set; }
}
public List<CustomType> ViewModelCollection { get; set; } = new List<CustomType>();
// skip
foreach (var data in GlobalDataCollection)
{
   ViewModelCollection.Add(new CustomType(999, data));
}

And the ViewModel binds the ViewModelCollection to the DataGrid of the View as a binding source.

Here are my question is:

When each ViewModel changes its own collection, what is the best way to reflect it to the GlobalDataCollection of the singleton class?

When 'Add', 'Remove', and 'Move' operations are executed on the ViewModel's own collection, the same operation must be applied to the GlobalDataCollection of the singleton class.

Aucun commentaire:

Enregistrer un commentaire