Wpf MVVM template - one data model, multiple view models

I am currently developing a simple editor as part of the learning process. Its basic hierarchical tree-like structure of polygon-line-point, which is implemented in the data model. I need to display this data in two views

First view: hierarchical data in a tree element Second view: visualized geometry on the screen

Following the MVVM pattern, I applied the model representation classes around the data model (the model point view, the linear model view, etc.). In the tree view, I use hierarchical data patterns to correctly display specific data. In the second view, I need to display the current state of the geometry, currently it’s just one shell of the view model around the polygon data class, which moves all the children and displays them in the onRender method. In this case, I use several presentation models using the same data as for completely different purposes.

There is a problem when I make some changes to the tree view model (for example, adding points), which leads to a change in the underlying data model. However, the second view model does not directly observe the data in the model view, it only updates the visualization if I make a modification through my models. Is there an elegant solution for updating both models at the same time?

+3
source share
2 answers

I decided this by introducing the Presenter. Here's basically how it works:

  • My domain model contains some representation of the process (call it a task, a workflow, or something else). It contains “business logic” for the actual actions that you are doing.
  • .
  • ViewModel (, , ViewModels), ViewModel ( ).
  • ViewModels .
  • ViewModels . - , , . (, , ).
  • Presenter ViewModels, .

, ViewModel, ViewModels. ViewModels IViewModelWithChildren, IEnumerable<IViewModel>, ViewModel , ViewModel.

, ( Presenter), . , , nice MessageViewModel .

+2

, PropertyChanged . , , .

, , , Prism.

0

Source: https://habr.com/ru/post/1759227/


All Articles