WPF MVVM design pattern

  MVVM is short for Model-View-ViewModel. It is essentially an improved version of MVC. In the MVVM mode, View encapsulates UI and UI logic, ViewModel encapsulates presentation logic, and Model encapsulates business logic and data.
Insert picture description here

View class

  The responsibility of the View is to define the structure and appearance on the screen. In a perfect situation, the code behind of the View only contains a constructor that calls InitializeComponent, but usually, the code behind contains UI logic code that is difficult to express in XAML tags. UI logic code refers to code that is related to the display behavior and does not contain any other logic, such as complex animation or code that directly manipulates the visual elements in the View.

  View interacts with its data context through data binding. In the MVVM mode, the data context of the View is placed in the ViewModel. The ViewModel implements the properties and commands required by the View data binding, and at the same time notifies the view of its status through change notification events The change. The typical relationship between View and ViewModel is one-to-one and many-to-one.

  • To sum up, View has the following characteristics:

View is a visual element. For example, window, page, usercontrol, and view define the controls contained in it and the layout and style of these controls.

View refers to its ViewModel through the DataContext property, and the controls in the View are bound to the property and command in the ViewModel.

View can customize the behavior bound to ViewModel. For example, View can use a converter to format the data displayed on the UI, and it can also use validation rules to validate user input.

View can define and process the behavior of UI visualization, such as animation or transformation caused by state changes or user interaction in ViewModel.

View’s codebehind can define UI logic to implement visual behavior that is difficult to express in XAML or directly manipulate the behavior of visual elements in View.

ViewModel class

  The ViewModel defines the presentation logic and the data required by the View. It does not reference the View and knows nothing about the realization of the View. The ViewModel implements the properties and commands that the View uses to bind, and uses change notification events to notify the view of its state changes, properties And commands define the functions required by the UI, and the View determines how to display these functions.

  The responsibility of the ViewModel is to coordinate the interaction between the View and the required Model. Normally, the ViewModel and the Model have a one-to-n relationship. The ViewModel can choose to expose the Model class directly to the View class, so that the controls in the View can be directly bound to the Model In this case, the Model class needs to be designed to support data binding and the corresponding notification mechanism.

  • To summarize, ViewModel has the following characteristics:

ViewModel encapsulates presentation logic to support the use cases required by the application.

  ViewModel does not directly reference View, it implements the properties and commands that View uses to bind, and uses change notification events to notify the view of changes in its state.

ViewModel coordinates the interaction between View and Model.

The ViewModel can define the logical state required for the View to render.

Model class

  Model encapsulates business logic and data. It represents the performance of the domain model of the application on the client, including both client domain entities and client domain logic, so it is a very broad concept.

  • To sum up, Model has the following characteristics:

  Model encapsulates business logic and data. It is responsible for managing application data and encapsulating business logic and verification logic to ensure data consistency.

Model does not directly reference View and ViewModel.

The Model class usually provides properties or sets of change notification events to facilitate binding with View.

  The Model class usually uses the IDataErrorInfo interface and the INotifyDataErrorInfo interface to provide data validation and error reporting.

  • Novice learning, please advise

Guess you like

Origin blog.csdn.net/qq_43562262/article/details/111080354