Tales from the Smart Client

John Gossman's observations on Avalon development

Introduction to Model/View/ViewModel pattern for building WPF apps

Model/View/ViewModel is a variation of Model/View/Controller (MVC) that is tailored for modern UI development platforms where the View is the responsibility of a designer rather than a classic developer.  The designer is generally a more graphical, artistic focused person, and does less classic coding than a traditional developer..  The design is almost always done in a declarative form like HTML or XAML, and very often using a WYSIWYG tool such as Dreamweaver, Flash or Sparkle.  In short, the UI part of the application is being developed using different tools, languages and by a different person than is the business logic or data backend.  Model/View/ViewModel is thus  a refinement of MVC that evolves it from its Smalltalk origins where the entire application was built using one environment and language, into the very familiar modern environment of Web and now Avalon development.

Model/View/ViewModel also relies on one more thing:  a general mechanism for data binding.   More on that later.

The Model is defined as in MVC; 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. 

The View in Model/View/ViewModel consists of the visual elements, the buttons, windows, graphics and more complex controls of a GUI.  It encodes the keyboard shortcuts and the controls themselves manage the interaction with the input devices that is the responsibility of Controller in MVC (what exactly happened to Controller in modern GUI development is a long digression...I tend to think it just faded into the background.  It is still there, but we don't have to think about it as much as we did in 1979).  The View is almost always defined declaratively, very often with a tool.  By the nature of these tools and declarative languages some view state that MVC encodes in its View classes is not easy to represent.  For example, the UI may have multiple modes of interaction such as "view mode" and "edit mode" that change the behavior of the controls or the look of the visuals, but these modes can't always be expressed in XAML (though triggers are a great start).  We will solve this problem later.

At this point data binding comes into play.  In simple examples, 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.  For example, a boolean in the Model can be data bound to a CheckBox, or a string field to a TextBox.

In practice however, only a small subset of application UI can be data bound directly to the Model, especially if the Model is a pre-existing class or data schema over which the application developer has no control.  The Model is very likely to have a data types that cannot be mapped directly to controls.  The UI may want to perform complex operations that must be implemented in code which doesn't make sense in our strict definition of the View but are too specific to be included in the Model (or didn't come with the pre-existing model).  Finally we need a place to put view state such as selection or modes.

The ViewModel is responsible for these tasks.  The term means "Model of a View", and can be thought of as abstraction of the view, but it also provides a specialization of the Model that the View can use for data-binding.  In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model. 

I will develop these ideas, and describe in particular how to bind the View to Commands on the ViewModel in later posts.  But the quickest way to clarify this pattern is to provide some examples:

 

 

 

The above picture shows three editing panels in the Sparkle UI.  Each has been developed using the Model/View/ViewModel pattern.   The simplest is the Library Panel at the top.  The Model is a list of assemblies (each an instance of System.Reflection.Assembly), and associated with each list of assemblies is a list of controls.  The View is one of our Panel chrome controls and a series of Styles and DataTemplates that expose the assembly list in a ComboBox and the list of controls in a ListBox.  We data bind the caption of the ComboBox directly to the name of the Assembly and the items of the listbox take their text from the name of the Control.  The ViewModel has as state the currently selected Assembly and exposes Commands for inserting a control into the scene.   Selection is one of the most common components of a ViewModel.  But since controls have selection, you may wonder why selection isn't left in the View instead.  This is done because often many controls in the view need to coordinate based on a single selection.  It is easier to bind to a single representation of selection in the ViewModel than coordinate all the different controls in the view.  In the Library, the selected Assembly determines what is selected by the ComboBox and also what list data is displayed by the ListBox.  Additionally, the designer could decide to switch to using a ListBox for the assemblies and a ComboBox for the control list without copying over selection coordination logic from his original view.

The Appearance panel has as its Model the selected shapes or controls in Sparkle's editing area.  The View has a ListBox that displays the interesting properties on the selection (basically all the Pen and Brush properties), buttons that determine whether the Brush or Pen is solid, gradient etc. and a color spectrum for editing color components.  The ViewModel includes what property is selected, what gradient stop is selected when editing a gradient, data converters for mapping colors to text values and to positions in the color spectrum, and commands for changing the Pens and Brushes being edited.  In this case the Model was given to us by Avalon, the View could easily be changed to something radically different, and the ViewModel provides an abstract representation for the reusable parts of this UI.

The final example is our Project panel.  Here the Model is an MSBuild Project...again a Model class that was pre-existing.  The View is a tree control, scrolling area, and contains context menus.  The ViewModel adapts MSBuild concepts that were designed without Avalon in mind (and work perfectly fine from the command line) so we can data bind to them, and again contains selection and commands. 

Once you've grokked the Model/View/ViewModel pattern any UI problem is quickly stated in its terms.  In fact, the entire Sparkle UI is defined using the pattern.  The "selected shapes or controls in the editing area" that is the Model for the Appearance panel is itself a ViewModel concept from our Scene editor.  The layout of Panels inside Sparkle has a Model consisting of a list of all registered panels, a View made up of a Grid with splitters that position the views, and a ViewModel that includes what panels are currently visible and what logical containers they are in (editing area, right, left, bottom trays).

More later...

Published Saturday, October 08, 2005 6:51 PM by JohnGossman

Comments

 

nar said:

What's that collapsable panel that you use as a container for your editing panels there? Is it included with WPF?
October 11, 2005 9:57 AM
 

JohnGossman said:

The panel is custom. I don't know if WPF will contain something similar by the time we release.

October 11, 2005 10:55 AM
 

Chris Ortman said:

I really like this pattern, it seems very logical and extensible. I'm definately looking forward to the power of WPF...but sigh we won't have it generally available for sometime yet. I was wondering if you had ever tried to implement this same pattern using winforms databinding? I have attempted it, but ended up getting stuck when it came to representing hierchial data and needing to deep bind ie bind to something like Customer.Name.First.
October 11, 2005 3:47 PM
 

Some Guy said:

What is it with you Microsoft people? "The Controller has faded"? WHAT PLANET ARE YOU ON? If I ever imagined that you might understand object-oriented programming one of these years, that hope is gone.

Take a couple of months, study Apple's Cocoa framework, learn how they implemented bindings, and save your company and all of your developers man-years of wasted effort.

The key concepts you need to understand are MVC, Messaging, the Target-Action pattern, the Notification pattern, and the Delegation pattern. The key implementations you need to understand are Key-Value Coding, Key-Value Observing, and Keypaths.
October 12, 2005 4:41 AM
 

Mike Scott said:

Some Guy: this is just a terminology mismatch. What John calls a ViewModel would be called a view-controller in Cocoa. Check out the Cocoa Design Patterns Guide p 34(http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDesignPatterns/CocoaDesignPatterns.pdf):

"The controller layer frequently contains many lines of code. To make this quantity of code more manageable, it is sometimes useful to subdivide the controller layer further into “model-controllers” and “view-controllers”.

A model-controller is a controller that concerns itself mostly with the model layer. It “owns” the model; its primary responsibilities are to manage the model and communicate with any view-controller
objects. Action methods that apply to the model as a whole will typically be implemented in a model-controller. The document architecture provides a number of these methods for you; for example, NSDocument automatically handles action methods related to saving files.

A view-controller is a controller that concerns itself mostly with the viewlayer. It “owns” the interface (the views); its primary responsibilities are to manage the interface and communicate with the
model-controller. Action methods concerned with data displayed in a view will typically be
implemented in a view-controller."
October 24, 2005 7:46 AM
 

The Orbifold»Blog Archive » WPF patterns : MVC, MVP or MVVM or…? said:

December 12, 2006 6:00 AM
 

Ask Dr. WPF said:

Dear Dr. WPF, I am about to begin leading a rather large WPF project. I’ve worked on plenty of development efforts involving Windows Forms projects and even a few Win32 projects, but this ...

September 30, 2007 4:49 AM
 

Quantum Bit Designs » Blog Archive » WPF Application Design and Architecture said:

January 20, 2008 2:59 PM
 

Ask Dr. WPF said:

I just saw a *GREAT* presentation by Josh Smith on using the Model View Controller (MVC) pattern to develop WPF applications. Josh did an awesome job of breaking down the different pieces of t ...

January 23, 2008 8:22 PM
 

Border Crossing Stats » Tales from the Smart Client : Introduction to Model/View/ViewModel … said:

March 13, 2008 3:42 AM
 

Ask Dr. WPF said:

Robby has just fired this shot in The Great Templating War Debate of 2008. I don't often disagree with Robby on platform issues, but... I am now compelled to reply with an opposing viewp ...

March 24, 2008 9:49 PM
 

Ask Dr. WPF said:

Oh look... I did it again! I promised to write ‘G’ is for Generator and then I come out with ‘I’ is for Item Container. I’m like a bad TV series that just k ...

March 26, 2008 2:46 AM
 

Steve Morgan's Technology Spot said:

It’s been a while, but I’ve started work on a new pet project. Significantly, it’s my first foray into Windows Presentation Foundation.

April 15, 2008 4:50 PM
 

Mike Hillberg's Blog on Wpf (.Net and Silverlight) said:

Like a lot of people, I’ve developed software professionally for a lot of different environments: PC

May 21, 2008 6:12 PM
 

Craig Shoemaker said:

Listen to the show here! John Gossman , Microsoft Architect for WPF and Silverlight, discusses his experiences

May 22, 2008 1:00 PM
 

My Technobabble said:

Several drops ago we introduced a ViewModel composition spike. The purpose of this spike was to introduce

May 24, 2008 12:09 PM
 

Glenn Block said:

Several drops ago we introduced a ViewModel composition spike. The purpose of this spike was to introduce

May 24, 2008 12:10 PM
 

Community Blogs said:

Several drops ago we introduced a ViewModel composition spike. The purpose of this spike was to introduce

May 24, 2008 1:05 PM
 

Using ViewModels and DataTemplates to compose your UI | Developer Home said:

May 29, 2008 6:01 AM
 

Model-see, Model-do, and the Poo is Optional : Silverlight said:

May 31, 2008 12:16 PM
 

YARI (Yet another refeactoring interation) « V-Base Development Blog said:

June 4, 2008 6:56 AM
 

.NET & Funky Fresh said:

First I'd like to say that I'm stoked about the work the Prism team has been doing.  I have

June 6, 2008 9:24 AM
 

wpf mvvm said:

June 13, 2008 3:55 AM
 

DEVELOPMENT SITE - NOT MY PUBLIC BLOG said:

Introduction to the Model/View/ViewModel Pattern for Building WPF Apps Model/View/ViewModel is a variation of Model/View/Controller (MVC) that is tailored for modern UI development platforms where the View is the responsibility of a designer rather than

June 28, 2008 11:40 PM
 

CodeItNow » Nothing To Report said:

July 1, 2008 2:01 AM
 

Quantum Bit Designs » Blog Archive » WPF Application Design and Architecture said:

July 22, 2008 11:19 PM
 

Alkampfer’s Place » Blog Archives » Binding combobox to Enum in silverlight said:

July 29, 2008 12:18 PM
 

Elise's blog said:

Après avoir participé à quelques projets WPF depuis maintenant un peu plus d'un an, je suis dans une

August 6, 2008 9:39 AM
 

Elise's blog said:

Après avoir participé à quelques projets WPF depuis maintenant un peu plus d'un an, je suis dans une

August 12, 2008 7:05 AM
 

Silverlight Travel » Blog Archiv » Model-View-ViewModel (M-V-VM) pattern said:

August 12, 2008 5:48 PM
 

Chris Donnan : Programming - Brooklyn Style » Blog Archive » WPF Model-View-ViewModel, I see the wisdom said:

September 1, 2008 6:24 PM
 

Caliburn Alpha fuer WPF und Silverlight said:

October 13, 2008 12:54 AM
 

Model/View/ViewModel pattern: useful links | DavideZordan.net said:

October 25, 2008 8:13 AM
 

Fire collection updated events from a special Silverlight ItemsControl « Developer Flotsam said:

November 10, 2008 5:01 PM
 

Beatriz Stollnitz » How can I expand items in a TreeView? - Part II said:

November 13, 2008 8:00 PM
 

Paul Stovell says… » Presentation: QMSDNUG Smart Client Patterns said:

November 18, 2008 7:55 PM
 

DarioSantarelli.Blog( said:

[WPF] Considerazioni su M-V-VM

November 30, 2008 10:22 AM
 

winkingzhang said:

Xaml的出现使得Win下非常有效的MVC变得复杂了很多很多,而WPF的发布使得Web程序和Win程序的鸿沟渐渐填平,对于模式的思考也渐渐浮出来:是使用复杂化的MVC,还是微软提出的MVP,抑或其他的模式。

November 30, 2008 10:59 PM
 

Adventures in Software » Blog Archive » Update Controls in WPF and Silverlight said:

December 10, 2008 5:22 PM
 

Jimmy Bogard said:

One common question when applying DDD is how to interpret other architecture’s concepts of a “model”

December 15, 2008 11:34 PM
 

JAPF » Blog Archive » Why should I use Model-View-ViewModel pattern said:

December 22, 2008 8:47 AM
 

Adventures in Software » Blog Archive » Presentation Model in WPF without INotifyPropertyChanged said:

December 27, 2008 9:33 PM
 

10 Minute Tutorial - JavaFX: Basic 2D Graphics and Animation : Die, AJAX! said:

January 1, 2009 3:29 PM
 

梦云 » 【翻译】如何展开TreeView中的节点?-第二部分 said:

January 2, 2009 10:50 PM
 

g??nl??k kay??tlar 2009-01-12 « Blogdivx said:

January 13, 2009 4:44 AM
 

g??nl??k kay??tlar 2009-01-12 « Blogdivx said:

January 13, 2009 5:13 AM
 

g??nl??k kay??tlar 2009-01-12 « Blogdivx said:

January 13, 2009 5:42 AM
 

g??nl??k kay??tlar 2009-01-12 « Blogdivx said:

January 13, 2009 6:07 AM
 

Robert Folkesson said:

Jag talar ofta med kunder och partners som vill använda vedertagna designmönster för att separera ansvaret

January 13, 2009 9:12 AM
 

Silverlight Travel » An introduction to MVVM in WPF with Karl Shifflett said:

January 20, 2009 1:15 AM
 

David Hill's WebLog said:

The ViewModel pattern (more formally called the Model-View-ViewModel pattern, but that’s way too

January 31, 2009 6:08 PM
 

Delay's Blog said:

When we created Silverlight Charting (background reading here and here ), we tried to make things as

February 4, 2009 3:29 AM
 

C and it’s sharp » Artikel über M-V-VM im MSDN Magazin said:

February 5, 2009 11:23 AM
 

kevin Mocha - WPF Model-View-ViewModel said:

February 6, 2009 10:40 AM
 

Episode 34: *Chirp and Witty - WPF Twitter Clients | Herding Code said:

February 6, 2009 3:40 PM
 

MVVM?????????????????????????????????????????????????????????????????????”WPF patterns : MVC, MVP or MVVM or…?” - SharpLab. said:

February 8, 2009 9:11 AM
 

Model-View-ViewModel in WPF Part I « Wes Aday’s Weblog said:

February 12, 2009 8:18 PM
 

C and it’s sharp » Tools für WPF und Silverlight Entwickler said:

February 14, 2009 1:19 PM
 

Community Blogs said:

In this article I will explain how to implement MVVM pattern in Silverlight. I was very overjoyed when

February 15, 2009 12:32 PM
 

kevin Mocha - WPF Model-View-ViewModel said:

February 16, 2009 5:46 PM
 

M-V-VM?????????????????????????????????????????????????????????2???????????????David Hill’s WebLog : The ViewModel Pattern??? - SharpLab. said:

February 20, 2009 7:17 AM
 

Craig Shoemaker said:

Over the years I have spent a fair amount of time thinking about design patterns surrounding the presentation

February 26, 2009 9:54 AM
 

Bryant Likes's Blog said:

You may have noticed the new look for the Twilight Twitter Badge on my blog a few weeks ago. I wanted

February 26, 2009 2:16 PM
 

Silverlight Travel » Twilight 1.5: Multiple Views with MVVM said:

February 27, 2009 4:56 AM
 

geff zhang said:

早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr

February 28, 2009 10:51 PM
 

MVVM: How to « Steveluo’s Blog said:

March 1, 2009 10:25 AM
 

Model-View-ViewModel (MVVM) Screencast for Silverlight and WPF « vincenthome’s Tech Clips said:

March 2, 2009 1:26 PM
 

Virtual Dreams » WPF: dicas para começar com MVVM said:

March 3, 2009 6:54 PM
 

Burbujas en .NET said:

Hace algún tiempecillo escribí un artículo para el e-zine de raona , que enviamos

March 12, 2009 6:12 AM
 

Embedded Musings said:

The .NET Micro Framework SDK comes with a customizable emulator that allows extensions to the emulated

March 15, 2009 12:19 AM
 

[MVVM + Mediator + ACB = cool WPF App] – The MVVM « C# Disciples said:

April 8, 2009 8:51 AM
 

Vincent Sibal's Blog said:

Launching a custom dialog for editing on the DataGrid is another somewhat common request that I see from

April 10, 2009 10:02 AM
 

MSDN geekSpeak said:

Here are follow up resources for Paul Sheriff’s geekSpeak on WPF for the Business Developer . Be sure

April 27, 2009 12:19 PM
 

Resources for geekSpeak: WPF for the Business Programmer with Paul Sheriff | Microsoft Share Point said:

April 27, 2009 12:38 PM
 

Resources for geekSpeak: WPF for the Business Programmer with Paul Sheriff | Coded Style said:

April 27, 2009 1:08 PM
 

Resources for geekSpeak: WPF for the Business Programmer with Paul Sheriff | Coded Style said:

April 27, 2009 1:08 PM
 

Resources for geekSpeak: WPF for the Business Programmer with Paul Sheriff | ASP NET Hosting said:

April 27, 2009 1:28 PM
 

POKE 53280,0: Pete Brown's Blog said:

Thanks again to Marc Schweigert for hosting last night’s DevDinner in Reston. My blog is at www.irritatedVowel.com/Blog

April 30, 2009 11:48 AM
 

My Technobabble said:

http://www.flickr.com/photos/richardmoross/3064808115/ No we’re not actually making a movie about View

May 4, 2009 11:58 PM
 

???View Model??? - the movie, cast your vote. | Coded Style said:

May 5, 2009 1:20 AM
 

Glenn Block said:

http://www.flickr.com/photos/richardmoross/3064808115/ No we’re not actually making a movie about

May 5, 2009 3:00 AM
 

???View Model??? - the movie, cast your vote. | Coded Style said:

May 5, 2009 3:23 AM
 

???View Model??? - the movie, cast your vote. | Coded Style said:

May 11, 2009 11:16 PM
 

???View Model??? - the movie, cast your vote. | Coded Style said:

May 11, 2009 11:16 PM
 

.NET & Funky Fresh said:

Recently Glenn Block asked some questions of the community concerning what support Microsoft should offer

May 18, 2009 10:55 AM
 

XAML vs C# (Composite Collection) « Hoa Chau’s weblog said:

May 19, 2009 4:01 PM
 

XAML vs C# (Composite Collection) – repost « Hoa Chau’s weblog said:

May 20, 2009 12:17 AM
 

Two-Way Data Binding in Cocoa « Cocoa Convert said:

June 1, 2009 1:16 AM
 

Raffaeu (Raffaele Garofalo Blog's) said:

WPF e il View model, qualche risorsa utile.

June 4, 2009 9:41 AM
 

Tales from the Smart Client Introduction to Model View ViewModel | Insomnia Cure said:

June 8, 2009 5:52 PM
New Comments to this post are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker