banner



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

  1. using  System.ComponentModel;

User.cs

  1. public class  User : INotifyPropertyChanged
  2. {
  3. private int  userId;
  4. private string  firstName;
  5. private string  lastName;
  6. private string  city;
  7. private string  state;
  8. private string  country;
  9. public int  UserId
  10.     {
  11. get
  12.         {
  13. return  userId;
  14.         }
  15. set
  16.         {
  17.             userId = value;
  18.             OnPropertyChanged("UserId" );
  19.         }
  20.     }
  21. public string  FirstName
  22.     {
  23. get
  24.         {
  25. return  firstName;
  26.         }
  27. set
  28.         {
  29.             firstName = value;
  30.             OnPropertyChanged("FirstName" );
  31.         }
  32.     }
  33. public string  LastName
  34.     {
  35. get
  36.         {
  37. return  lastName;
  38.         }
  39. set
  40.         {
  41.             lastName = value;
  42.             OnPropertyChanged("LastName" );
  43.         }
  44.     }
  45. public string  City
  46.     {
  47. get
  48.         {
  49. return  city;
  50.         }
  51. set
  52.         {
  53.             city = value;
  54.             OnPropertyChanged("City" );
  55.         }
  56.     }
  57. public string  State
  58.     {
  59. get
  60.         {
  61. return  state;
  62.         }
  63. set
  64.         {
  65.             state = value;
  66.             OnPropertyChanged("State" );
  67.         }
  68.     }
  69. public string  Country
  70.     {
  71. get
  72.         {
  73. return  country;
  74.         }
  75. set
  76.         {
  77.             country = value;
  78.             OnPropertyChanged("Country" );
  79.         }
  80.     }
  81.     #region INotifyPropertyChanged Members
  82. public event  PropertyChangedEventHandler PropertyChanged;
  83. private void  OnPropertyChanged( string  propertyName)
  84.     {
  85. if  (PropertyChanged != null )
  86.         {
  87.             PropertyChanged(this , new  PropertyChangedEventArgs(propertyName));
  88.         }
  89.     }
  90.     

0 Response to "Wpf Apps With The Model-view-viewmodel Design Pattern Pdf"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel