Example of Two Way Data binding in WPF MVVM

Take the new project in that adds three folder naming as Model, ViewModel and  View. See the below screen. We are giving the project name as WPFPurchaseMaster.



What are the Step Adding of classes in WPF MVVM?

Step1: Right Click on Model  folder  àAdd àAddNewItem select the class file name them as Customer

Step2: Right Click on ViewModel  folder àAdd àAddNewItem select the class file name them as CustomerViewModel

Step3: Click on View folder àAdd àAddNewItem select the Window(WPF extension as .xaml)  file name them as PurchaseOrder

Structure should be look like below image


Write Code on respective classes each shown below with namespace

Customer.Cs:

using System.ComponentModel; for INotifyPropertyChanged property
INotifyPropertyChanged used for handling notification when any change of data in the controls.

For example when we change the text box value then it notifies the respective properties bonded to that control.



Customer Model


CustomerViewModel.cs

Add Namespace  using WPFPurchaseMaster. Model;

Customer View Model


PurchaseOrder.Xaml

Windows Design


Namespace form

Grid Design

Grid Design


Change the above code StartupUri=”MainWindow.xaml” to
StartupUri=” View/PurchaseOrder.xaml” 

Run the application the output should be like below


When select grid row, data of grid shown on above text box control like below screen

You can change any field from above control like we can change customer name, contact number, Discount etc...

Please feel free  to share your quarries in comment.

Post a Comment

Previous Post Next Post