I am new to WPF and MVVM, and I am a bit confused on how to use model and viewModel for what I need. The application will have a global "World" object, that will contain a collection of objects of various custom classes that will be created, destroyed and updated continuously by background threads that listen to ports and sockets, being this the body of the application logic. This collection of objects needs to remain static throughout the execution of the application, regardless of page navigation, and be accessible from custom classes and editable from the UI (there will be a page for an overview of the "world" status, as well as pages for detailed views, editing of objects and configurations).
From what I've read, I gather this data should be maintained by the model, and the ViewModel should access it to prepare it for the View. Does this mean that, for every "world" custom class that I create and need to be viewed or edited by the user, there should be a ViewModel to match?
Looking at code samples and tutorials, I see that viewModels objects are linked to views in the XAML code and instantiated when said view page is loaded (and destroyed on exit). How do I link that viewModel object to a specific and existing Model object, from which it should obtain all data and to which the UI input data has to be saved?
To summarize:
- World object collection is created in application start (app or mainWindow scope). For example objects of class Task are created.
- Background processes are initiated, that access the tasks collection and do things depending on what they find. (also they can change data, so it has to notify the modelview to display the changes).
- GUI navigation is initiated, and controls are created dynamically, that view and edit the data in the world objects. For example, display controls to manage multiple Tasks.
How do I link the Model objects (e.g. a specific task) with the viewModel? On XAML? in the code behind of the viewModel, with some kind unique identifier for each Model instantiation? When adding the controls dinamically in codebehind, passing the model object instance to the viewModel? What's the best way for the model to notify changes to de viewModel and viceversa?
I would appreciate any guidance or a reference to a code sample that solves a situation like this.
Thanks in advance