This is a mirror of official site: http://jasper-net.blogspot.com/

Getting started with MVVM in WPF

| Sunday, May 9, 2010
I’ve been working with MVVM and WPF for a couple of weeks now. I decided to log what I’ve learned here. Here goes a getting started tutorial with MVVM.

The Model-View-ViewModel pattern was introduced by John Gossman to effectively utilize the functionality of WPF. Since then, MVVM has been used in a number of WPF applications with very impressive results.
MVVM has three components:

Model: It is your data or classes that represent entities in your application. It normally contains no WPF-specific code.
View: This is the User Interface element visible to the user. Its DataContext is its ViewModel.
ViewModel: It contains all the data that needs to be displayed and procedures to modify the model at will. The magic about MVVM is that the ViewModel knows nothing about the View.
You see that this is very loosely coupled. The View knows the ViewModel but the ViewModel does not know the View. You can very easily replace the View without affecting the ViewModel. This is very useful in Developer/Designer teams where the Developer improves the ViewModel and the Designer enhances the View.

The fact that the ViewModel does not know anything about the View comes as a bit of surprise. There is one more surprise – a typical View in MVVM does not need have a code-behind (except for the general boiler-plate code that calls the InitializeComponent() method from the constructor)!

You may be wondering how the view updates itself when the ViewModel changes and how it handles user interaction like button clicks etc. This is what makes MVVM specific to WPF.

The controls in the View bind themselves to the corresponding properties in the ViewModel. The changes in ViewModel will be reflected in the view, thanks to Data Binding in WPF. (Otherwise we would have had to handle every event and then update the view accordingly.)

Read more: CodingTales.com

Posted via email from jasper22's posterous

0 comments: