Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » WPF » MVVM in WPF

MVVM in WPF

In this article, I'll review some of those best practices for designing and implementing client applications with WPF. By leveraging some core features of WPF in conjunction with the Model-View-ViewModel (MVVM) design pattern.

Author Rank :
Page Views : 17698
Downloads : 474
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MVVMApplication.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


As the software world continues to adopt WPF at an increasing rate, the WPF community has been developing its own ecosystem of patterns and practices. In this article, I'll review some of those best practices for designing and implementing client applications with WPF. By leveraging some core features of WPF in conjunction with the Model-View-ViewModel (MVVM) design pattern.

Why MVVM?

Once a developer becomes comfortable with WPF and MVVM, it can be difficult to differentiate the two. MVVM is well suited to the WPF platform, and WPF was designed to make it easy
to build applications using the MVVM pattern.

The most important aspect of WPF that makes MVVM a great pattern to use is - the data binding infrastructure, which allows the View (the presentation of to the user) to be separated from the data and the logic. The View can be designed in Expression Blend while the ViewModel can be developed in Visual Studio .NET. And the other features of WPF are data templates and the resource system.

MVVM Architecture:

MVVM1.gif

Difference between MVP, MVC and MVVM:

Mostly it is very confusing to know the difference between MVP (Model-View-Presenter), MVC (Model-View-Controller) and MVVM (Model-View-View-Model). Here I am trying to clear all the 3 with the help of diagram:

MVP (Model-View-Presenter)

In the MVP pattern the User sends the input to the view, the view forward it to presenter and presenter than modify the view or the model depending on the type of user action. The view and the presenter are tightly coupled through bi-directional relationship. The model does not know about the presenter. The view itself is passive, thats why it's called presenter pattern, since the presenter pushes the data into the view.

Diagram:

MVVM2.gif

MVC (Model-View-Controller:

In this pattern there is only one controller that gets all the inputs directly, it modifies the data in the model depending upon the type of the input. Both the model and the view are created by the controller. The view only knows about the model, but the model does not know about any other objects.

Diagram:


MVVM3.gif

MVVM (Model-View-ViewModel):

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. The View contains the visual controls that will be shown to the user and can also contain animations, navigation aspects, themes, and other interactive features for the purpose of the visual presentation. The View is data bound directly to the Model. Parts of the Model are simply displayed in the view by one-way data binding. Other parts of the model can be edited by directly binding controls two-way to the data. It acts as a bridge between your software and its users.

MODEL: Model is responsible for exposing data in a way that is easily consumable by WPF. It must implement INotifyPropertyChanged and/or INotifyCollectionChanged as appropriate. When data is expensive to fetch, it abstracts away the expensive operations, never blocking the UI thread. It is the data or business logic, completely UI independent, that stores the state and does the processing of the problem domain. The Model is written in code or is represented by pure data encoded in relational tables or XML.

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. The ViewModel is the glue between the View and the outside world. The ViewModel is what the View is bound to. It provides a specialization of the Model that the View can use for data-binding.

Here is a simple illustration how all the 3 relate to each other:

MVVM4.gif

MVVM in WPF:

MVVM in WPF relies heavily on WPF's excellent binding capabilities to remove some of this plumbing code, leaving us to focus on the logical model of our view or the ViewModel, and our application. The goal of MVVM is to separate the design of the application from the data and the functionality, while supporting multiple development languages (e.g. C#/XAML) and taking full advantage of rich databinding.

Here is the Running window of our Application Using MVVM in WPF:

MVVM5.gif

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Excellent Article on MVVM !! by Manikavelu On February 24, 2011
Good Explanation with clear picture.
Reply | Email | Modify 
Suggestion by Pritesh On March 8, 2011
if you don't mind, As per my knowledge or Reading and as per link at the end of my comment MODEL does not Implement INotifyPropertyChanged and/or INotifyCollectionChanged. it is the task of VIEWMODEL to Implement INotifyPropertyChanged and/or INotifyCollectionChanged. 1. http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090006 2. http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel
Reply | Email | Modify 
VS2010 can't load Main Window Xaml by Larry On August 10, 2011
VS2010 complains about two lines. xmlsn:views="clr-namespace:MVVMApplication.Views" <views:EmployeeView x:Name="EmployeeView"/> // believe this line is a problem because of the above error. The error is Error 1 Undefined CLR namespace. The 'clr-namespace' URI refers to a namespace 'MVVMApplication.Views' that is not included in the assembly. T:\Temp\MVVMApplication\MVVMApplication\MVVMApplication\MainWindow.xaml 4 21 MVVMApplication It does build and run. Can someone shed some light on this? The end result is that the designer does not display the UI which makes working on a UI difficult to say the least. Thanks, Larry O'Heron <Window x:Class="MVVMApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:views="clr-namespace:MVVMApplication.Views" Title="MVVM in WPF" Height="375" Width="600" Loaded="Window_Loaded"> <Grid Background="LightGray"> <views:EmployeeView x:Name="EmployeeView"/> </Grid> </Window>
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.