Wpf Apps With The Model-view-viewmodel Design Pattern Pdf
- Updated date May 21, 2019
- 713k
- 18
This article describes the basic use and functionality of the MVVM pattern in WPF.
This article describes the basic use and functionality of the MVVM pattern in WPF.
The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft which is specialized in the Presentation Model design pattern. It is based on the Model-view-controller pattern (MVC), and is targeted at modern UI development platforms (WPF and Silverlight) in which there is a UX developer who has different requirements than a more "traditional" developer. MVVM is a way of creating client applications that leverages core features of the WPF platform, allows for simple unit testing of application functionality, and helps developers and designers work together with less technical difficulties.
VIEW: A View is defined in XAML and should not have any logic in the code-behind. It binds to the view-model by only using data binding.
MODEL: AModel is responsible for exposing data in a way that is easily consumable by WPF. It must implement INotifyPropertyChanged and/or INotifyCollectionChanged as appropriate.
VIEWMODEL: A ViewModel is a model for a view in the application or we can say as abstraction of the view. It exposes data relevant to the view and exposes the behaviors for the views, usually with Commands.
Getting Started
- Creating a WPF Project. Open Visual Studio 2010.
- Go to File => New => Project
- Select Window in installed templates
- Select WPF Application
- Enter the Name and choose the location.
- Click OK
Now create three folders in root application. Name should be Model,View,ViewModel and now add a new class in Model folder. My class name is User and add this namespace
- using System.ComponentModel;
User.cs
- public class User : INotifyPropertyChanged
- {
- private int userId;
- private string firstName;
- private string lastName;
- private string city;
- private string state;
- private string country;
- public int UserId
- {
- get
- {
- return userId;
- }
- set
- {
- userId = value;
- OnPropertyChanged("UserId" );
- }
- }
- public string FirstName
- {
- get
- {
- return firstName;
- }
- set
- {
- firstName = value;
- OnPropertyChanged("FirstName" );
- }
- }
- public string LastName
- {
- get
- {
- return lastName;
- }
- set
- {
- lastName = value;
- OnPropertyChanged("LastName" );
- }
- }
- public string City
- {
- get
- {
- return city;
- }
- set
- {
- city = value;
- OnPropertyChanged("City" );
- }
- }
- public string State
- {
- get
- {
- return state;
- }
- set
- {
- state = value;
- OnPropertyChanged("State" );
- }
- }
- public string Country
- {
- get
- {
- return country;
- }
- set
- {
- country = value;
- OnPropertyChanged("Country" );
- }
- }
- #region INotifyPropertyChanged Members
- public event PropertyChangedEventHandler PropertyChanged;
- private void OnPropertyChanged( string propertyName)
- {
- if (PropertyChanged != null )
- {
- PropertyChanged(this , new PropertyChangedEventArgs(propertyName));
- }
- }
0 Response to "Wpf Apps With The Model-view-viewmodel Design Pattern Pdf"
Post a Comment