WPF – How to maintain the consistency between the model and the view model in the MVVM mode?

Problem statement

I am writing a very basic WPF application to change the content of the configuration file. The data format is an XML file with a schema. I want to use it as a learning project for MVVM, so I have divided the code into two parts

> Model: C# class automatically generated from xsd.exe
>View Model: View-friendly representation of the model .
>View: Xaml and the empty code behind

I understand how View-Model makes View-binding a breeze. However, it does not leave View-Model<->The model semantics is very embarrassing ? Xsd.exe generates C# classes with multiple XML element arrays. However, at the V-VM level, you need Observable Collections.

Question:

Does this really mean Do I have to keep two completely different collection types representing the same data continuity?

What are the best practices for maintaining consistency between the model and the view model?

I am not a big expert, but I think so. The general idea is indeed spread through Binding The changes between the view and the viewModel are then propagated through events (in the Model -> ViewModel direction) or dependencies (in the other direction) to propagate the changes between the ViewModel and the Model.

I don’t know it What is the standard, but my understanding of MVVM is that ViewModel should save a reference to the model, so that when the user modifies the view, the ViewModel should call the corresponding code on the model. Conversely, the model should raise an event when it is modified, and the ViewModel Should update itself accordingly (ViewModel is the observer of the model).

Problem statement

I am writing a very basic WPF application Program to change the content of the configuration file. The data format is an XML file with a schema. I want to use it as a learning project for MVVM, so I have divided the code into two parts.

> Model: From xsd. C# class automatically generated by exe
>View Model: View-friendly representation of the model.
>View: Xaml and the empty code behind

I understand how View-Model makes View-binding become It’s easy. But, won’t you leave the View-Model<-> model semantics embarrassing? Xsd.exe generates C# classes with multiple XML element arrays. However, at the V-VM level, you need Observable Collections.

Question:

Does this really mean Do I have to keep two completely different collection types representing the same data continuity?

What are the best practices for maintaining consistency between the model and the view model?

I am not a big expert, but I think so. The general idea is indeed to propagate changes between the view and viewModel through Binding, and then through events ( In Model -> ViewModel direction) or dependent (in the other direction) to propagate changes between ViewModel and Model.

I don’t know what its standard is, but my understanding of MVVM is The ViewModel should save a reference to the model, so that when the user modifies the view, the ViewModel should call the corresponding code on the model. Conversely, the model should raise an event when it is modified, and the ViewModel should update itself accordingly (ViewModel is the observer of the model ).

Leave a Comment

Your email address will not be published.