claude hasler

claude hasler

  • NA
  • 14
  • 1.3k

MVVM multiple ViewModels and Views of Data

Apr 5 2019 12:37 PM
for a CAD like software I'm working on, my basic data model can for discussion be simplified to:
class Component{
   public string Name{get;set;}
   public List<Component> Children{get;set;}
 
A heirarchel data model with some descriptive properties.
 
In my program i wish to act on this data, namely change the data of the Components ( the name ) as well as the tree itself ( add / remove children).
Additionally i would like to display the data in several different forms:
 
-A TreeView of the tree structure
-A 3D View to visualize data
-A ListView for oversight over all Components
 
Currently my solution is to have a MainViewModel which holds the data (A "root" component) and then multiple SubViewModels ( TreeViewModel, 3dViewModel etc). Whenever a command is executed by the MainViewModel, say "AddComponent" the same Command is executed in the SubViewModels as well. While this works, it doesnt feel elegant to have to implement the same command in each ViewModel
 
What is the tried and true elegant way to display the same Model in several different Views, by several different ViewModels, and guarantee synchronicity across these? 
 
Regards