MVVM in Windows Presentation Foundation

The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft which specialized in the Presentation Model design pattern. is a software architectural pattern that helps in splitting the GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any specific model platform.

MVVM Architecture

i-24-53.jpg

VIEW: View is responsible for front-end look and feels and it doesn’t have any code supporting it. It is bound to the view-model by only using data binding. MODEL: Model is the domain object that represents the actual data we are dealing with VIEWMODEL: It acts as the bridge between the Model and View.

The view binds to properties on a ViewModel, which, in turn, exposes data contained in model objects and other state-specific to the view.

MVVM in Windows Presentation Foundation The Windows Presentation Framework (WPF) can leverage MVVM) pattern to build WPF applications in a much simpler way. Let’s look at a sample Project Architecture shown below:

i-24-54.png

VIEW: It is a visual element, such as a page, user control, window, or data template. It uses the data-binding capabilities of WPF to bind the properties defined in ViewModel to user controls that View is composed of.

User-events that are captured by the View are sent to the ViewModel through Commands. Now, these commands execute methods defined in the ViewModel which contains logic.

i-24-55.png

MODEL: Model Class is responsible for data representation. It is used to expose the data objects in an appropriate way that those can be easily managed and consumed by View and ViewModel.

i-24-56.png

VIEWMODEL ViewModel in the MVVM design pattern encapsulates the presentation logic and data for the view. It simply holds the data retrieved from the data layer. It is never tightly coupled with the View and notifies later, if any property changes in ViewModel. It might take inputs from View and place it on Model, or it might interact with a service to retrieve the model, then translate properties and place it on View. It also exposes methods, commands, and helps to maintain the state of the View, manipulates the Model as the result of actions on the View, and triggers events in View itself. ViewModel always updates Model and properties defined from View having UI level events mapped to commands in ViewModel by two-way data binding.

i-24-57.png

i-24-58.png

WPF Application using MVVM

The steps for creating a sample WPF application is shown below

  • Create a model class Employee

i-24-59.png

  • Create ViewModelBase

i-24-60.png

All ViewModel classes are inherited from ViewModelBase. Whenever property changes in View notification will get in ViewModel by implementing INotifyPropertyChanged . This implementation can be done in ViewModelBase and can reuse across all ViewModels which inherits from ViewModelBase,

  • Create command
  • There is no click event handling in code behind in MVVM pattern. We can use command property for buttons. The command logic can assign using delegate function

i-24-61.png

  • Create View
  • Now we can create a view and bind the properties of the view model into this view.

i-24-62.png

i-24-62-a.png

Here we have created User Control and include it in the main view as shown below.

i-24-63.png

  • Create ViewModel

Submit action will trigger submit logic in the Submit method using ‘SubmitCommand’. After submit action Employees property will change with modified employee list and it will notify in view using INotifyPropertyChanged implementation in ViewModelBase

i-24-64.png

i-24-65.png

i-24-66.png

By applying the MVVM pattern in WPF, we can build applications that are more streamlined to improve business application development process. The resulting applications can be maintained effectively and are better for testing as well.

Do you need cloud support, please contact Us!
Follow us on LinkedIn for future updates
Never Miss a Beat

Join our LinkedIn community for the latest industry trends, expert insights, job opportunities, and more!

close icon

We’re glad you’re here. Tell us a little about your requirement.

  • We're committed to your privacy. Zerone uses the information you provide us to contact you about our products and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy