I want to understand possible architectural level approaches that keep two views synchronised with a single model. Here I have a full implementation that uses MVVM Community Toolkit Messaging (see GitHub link below). What other ways of implementing this should be considered?
The setup
Imaging you have an application with two different view that need to synchronise the same model object between them. For example, let’s say we have a plotting application. In the left is UI to allow the user to control the axis limits and on the right is a plot view that displays data.
When the user edits the text box the plot view will update. When the user pans the plot the text boxes update. So this is bidirectional.
Moreover, let’s follow what happens when the user edits one of the settings text boxes.
- Edit Min text box with a new value
- View binding triggers the change handler on the Settings view-model.
- The change handler creates a new
AxisLimits
model object and updates itself with that value. - The view-model then sends a MVVM Community Toolkit Message telling all recipients of the change.
- The main view-model handles the message and distributes the new model object to all view-models.
GitHub
Take a look at a fully working example on GitHub, https://github.com/danieljfarrell/Avalonia-MVVM-Messaging-Example