WPF Model-View-ViewModel Toolkit

The Model-View-ViewModel toolkit is intended to introduce the Model-View-ViewModel design pattern for building WPF applications to the broad WPF developer community.

The toolkit includes:
  • A Visual Studio 2008 template (Visual C# Express 2008 also supported)
  • Documentation
    • General introduction to M-V-VM
    • Walkthrough using the VS template
  • A complete WPF application demonstrating the M-V-VM pattern

You can download the toolkit on the WPF Futures page.

Last edited May 2, 2009 at 6:51 AM by PatrickDanino, version 3

Comments

NikMb Apr 23, 2013 at 2:03 PM 
To add the template to VS 2010, download the template for VS 2008, create an empty project in VS2008, close VS 2008. Open the project with VS 2010 and convert it. Export the project as a template. You can either automaticall import into VS 2010 in which case it will appear under the root c# project templates. Or you just create the project template and manually place under \Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual C#\Windows\1033 to place it in the Windows sub section.

PatrickDanino Aug 24, 2011 at 11:21 PM 
As many of you have pointed out, the toolkit has not been updated for VS2010. The toolkit was really intended to be an introduction to MVVM, it was not intended to replace the community libraries available. In the hope that some will find it useful, I have created a VS 2010 extension that behaves as a hands-on-lab and walks through some of the basics. I'll mention that this is an unofficial release, given I no longer work on the WPF team. Link: http://visualstudiogallery.msdn.microsoft.com/af12ce3b-8337-4bbb-853f-9fdad7105ec8

jiewmeng Sep 24, 2010 at 2:48 AM 
How do I install this on VS 2010?

dewulf Jul 29, 2010 at 12:51 PM 
avira antivir guard detects ..\WPF M-V-VM Toolkit 0.1\Messenger Sample\Messenger.Demo\obj\Debug\RefactorMessenger.Demo.exe in WPF M-V-VM Toolkit 0.1.zip as Malware...

appWoW Jun 18, 2010 at 12:51 AM 
would be nice that the template install actually work for VS2010, but i suppose, copying in from sample works. hint... hint...

appWoW Jun 17, 2010 at 4:33 PM 
The commandreference class 'derives from Freezable to work around a limitation in WPF when data-binding from XAML' Is this still a problem with .net v4? tx

cosmic53 Apr 20, 2010 at 10:07 AM 
The last update to this page is almost one year old and there is no VS2010 question.
Are you still there?

jbe2277 Apr 11, 2010 at 7:30 PM 
@shaggygi: MVVM and MEF are a great team. See http://waf.codeplex.com

cosmic53 Apr 8, 2010 at 5:10 PM 
yes, will there be a versiom of the template for VS 2010?

robert_pl Apr 8, 2010 at 12:08 AM 
Will there be a version of the templates for VS 2010?

rogerrong Apr 7, 2010 at 3:53 AM 
I used to try the project template with VS 2008, any way to use the MVVM project template in VS 2010 RC target at .Net 4.0 ?

moriald Mar 17, 2010 at 5:07 PM 
I have the same problem. Is there a work-around for non-english version?
Any suggest are appreciated.

richterd Mar 9, 2010 at 11:11 PM 
Hello, the problem with non-english version of VS is not fixed in release Februar 2010...?

PatrickDanino Mar 9, 2010 at 6:58 PM 
@Nitro52: Thanks for taking the time to comment. Out of curiosity, are you using the Model-View Visual studio template to create your project as part of the sample walkthrough? The reason why there is no mention of hooking up the View Model and data context in the walkthrough is that it's actually done for you by the template. I definitely hear you in that it should have been made more explicit, not just to better illustrate what is happening, but for anyone who wishes to use the walkthrough without being forced to use the template.

leedgitar Mar 4, 2010 at 2:57 AM 
Hey all. I'm trying to run the included Messanger.Demo application without any luck. I go to login and it never succeeds. The only thing I see in the Output window in VS.net is "Messenger Connection Established". Anyone else have problems running it?

nitro52 Feb 8, 2010 at 1:48 AM 
Theres a syntax error in the code of the sample documentation "Model-View WPF Applications--Part 2, Walkthrough.docx"

When setting up ContactsDataSource it has the following lines

var contacts = System.Xml.Linq.XDocument.Load(filename).Root.Elements("Contact").Select(
x => new Contact()
{
FirstName = (string) x.Element("FirstName"),
LastName = (string) x.Element("LastName"),
Email = (string) x.Element("Email")
});

the line "x => new Contact()" shouldn't close the bracket at this point, instead it should be

var contacts = System.Xml.Linq.XDocument.Load(filename).Root.Elements("Contact").Select(
x => new Contact(
(string)x.Element("FirstName"),
(string)x.Element("LastName"),
(string)x.Element("Email")));


Also as other people have mentioned the when setting up the app.xmal.cs there are the following lines are not indicated as needing to be changed

ViewModels.MainViewModel mainViewModel = new ViewModels.MainViewModel();
.......
view.DataContext = mainViewModel;

Without these the sample does not work

Discofunk Jan 13, 2010 at 8:18 PM 
I deleted my last comment as it may have been unfair. However, I would still like to see an elegant solution to handling dialogs and dialog results as well as VM 2 VM and V 2 VM communication in an elegant way that isn't coupled. This toolkit offers an excellent starting framework but if you plan on using it be aware that, in its current state, you will need a lot of additional plumbing for scenerios with multiple views and view models that require communication and data passing (i.e. dialogs and results). Sacha Barber has a detailed series of articles on how he addresses some of these scenerios with his MVVM WPF Framework Cinch: http://www.codeproject.com/KB/WPF/Cinch.aspx and gives credit to others who have come up with inventive solutions.

camainc Dec 28, 2009 at 8:44 PM 
Where is the VB.Net version of the MVVM Toolkit?

rogerrong Dec 22, 2009 at 1:27 AM 
Anywhere I could find a sample of using the MVVM toolkit to build an app to manage a simple dataset?

Ciantic Dec 2, 2009 at 2:08 PM 
I've found a way to fix the bug in sample program where Tray Context Menu does not disappear, see patch in here: http://wpf.codeplex.com/WorkItem/View.aspx?WorkItemId=11022

PatrickDanino Nov 13, 2009 at 1:38 AM 
@rogerrong: Good question. The primary reason is one of dependencies. In .Net 3.0/3.5, the ObservableCollection class is defined in one of the primary WPF assemblies. Many existing applications that already have data models defined were built on top of .Net 2.0 and don't take WPF dependencies, so as to make it easy to reuse the data models in other places. The NotificationCollection class was to illustrate one way to work around the lack of ObservableCollection; if this isn't a concern for your particular scenario, then using ObservableCollection is perfectly fine, and in fact preferable if it saves from having to write and maintain more code. I'll also mention that the current plan is to type forward ObservableCollection to System.dll in .Net 4.0 for similar reasons. Thanks!

rogerrong Nov 11, 2009 at 10:25 PM 
There is a NotificationCollection class defined under the Models of the Messenger.Demo sample project. It looks similar to the ObservableCollection class. I am just wondering why bother to spend effort building such a big class instead of using the ObservableCollection straightforward.

Does the NotificationCollection provide any feature not covered by the ObservableCollection? If the NotificationCollection is a better option to be used by the Models classes, should it be included in the project template?

cwtews Oct 25, 2009 at 8:31 PM 
Please note the Walkthrough - part 2 has a small error. On Page 9 under the App OnStartup, the following lines need to be changed ... not just the addition of the call to LoadContacts (it's not really an error, just a lack of high-lighting ...)

ViewModels.MainViewModel mainViewModel = new ViewModels.MainViewModel();

// Load sample test data for demonstration purposes
mainViewModel.LoadContacts("Contacts.xml");

view.DataContext = mainViewModel;

ivom Aug 21, 2009 at 7:31 PM 
@sixmileal:
You are right on! To be frank, we are still in the process of nailing down exactly what such a foundation should contain. The fundamental patterns themselves are well understood. What we don't quite understand yet is where the sweet spot is when it comes to simplicity, framework support, toolability, as well as all the other fundamentals (deployment, localization, etc.) -- that's why we currently have different solutions satisfying niche needs (from MVVM toolkit, Prism, the SCE toolkit, etc.) as opposed to an overarching good-for-all-scenarios solution. I am sure we will get there eventually, so please keep this feedback coming.

Ivo

Larsalex Aug 21, 2009 at 7:35 AM 
For those having issues with the example in part2, check that in OnStartup in App.xaml.cs the line - which is not marked - 'view.DataContact = mainViewModel;' replaces the 'view.DataContext = new ViewModels.MainViewModel();' line. Otherwise the app is using a fresh instance without anything attached, which explains the empty Main Window.
This simple hands-on tutorial is very valuable for us new to WPF. Thanks a lot! Indeed simplicity is welcome in this jungle of frameworks and variations.

PatrickDanino Aug 17, 2009 at 11:49 PM 
@HtechDev: WPF supports navigation through the use of a 'Page' object, showing a navigation bar at the top of your application which makes it look a little bit like a web browser, but it's not a website (ASP.Net has its own MVC toolkit, if your intent is actually to build a website).

Having said that, this initial release of the M-V-VM toolkit doesn't include a framework for getting navigation to work "out of the box". We're aware of this, but it's good to know it's a feature you would find valuable and will consider it for a future release of the toolkit. Thanks!

HtechDev Aug 13, 2009 at 10:21 PM 
To be honest, I'm not 100% sure. I simply created a WFP Application within Visual Studio and changed Window.xaml to Page.xaml. Then changed the Xaml <Window> tag to <Page>. I ended up with a website to replace my old ASP.NET 2.0 version. But know I'd like to convert it to use the MV-VM design pattern.

PatrickDanino Aug 13, 2009 at 6:51 PM 
@HtechDev: I'm not sure what you have in mind when you say web page creation? Are you referring to Silverlight or ASP.Net by any chance?

HtechDev Aug 10, 2009 at 4:21 PM 
Is there a different MV-VM template I should use for web page creation rather than windows? Or does some type of checklist exist that will lead me quickly through the items I need to change? e.g. Change all "Windows" in xaml to "Page."

sixmileal Aug 5, 2009 at 3:31 PM 
Would I be incorrect in stating that the core problem being addressed by the ‘Model-View-X’ patterns is one of dependencies? To me, each one is simply a different approach at reconciling the dependency requirements that naturally exist between the different layers (Presentation-Presentation Logic-Model). If that is the case, would we not be better off resolving the much larger problem than coming up with variants? I think what is required is the missing ‘foundation’ member, WXF. A foundation member that would provide some of the facilities that are now being addressed through initiatives like Prism. If we leave out the UI aspect being solved with the RegionsManager, the remaining services are fundamental to all applications. And those being, a dependency injection/resolution mechanism (the solution to a lot of problems not just Model-View-X), a locator/loader service, and instancing/lifetime management. Some of these facilities are available for the WEB counterpart but not for the desktop. Perhaps I am being naïve in my viewpoint, but I see all Windows applications benefiting from how a Prism application is defined. Maybe WXF would provide a new meaning for ‘AppDomain’, where applications simply state their composition (modules), what their dependencies are, and the module instancing requirements.

PatrickDanino Aug 4, 2009 at 6:57 PM 
@ggovedar: I've actually been working on a more complex (and complete) M-V-VM sample that connects to a SQL CE database, so I hope we can release that sample soon. You can find some relevant information here: http://karlshifflett.wordpress.com/mvvm/wpf-line-of-business-introduction/
The model often interacts with a data layer, and I've used the singleton pattern to create a data source and use that throughout my application. Depending on the underlying technology and how complex you want to get, you could then write async methods to get data/update status/allow for cancelling. In general, I try to abstract data service implementations from the model classes, as it makes it easier to swap one out without affecting the other layers. Hope that helps!

PatrickDanino Aug 4, 2009 at 6:44 PM 
@investor5555: What edition of VS are you using, and is it in a non-english language?

PatrickDanino Aug 4, 2009 at 6:41 PM 
@WPFUser: The initial release of the template only works on English languages of VS. We're aware this is a big pain for the numerous people who don't use English software, and we plan on correcting that in the next release.

ggovedar Jul 29, 2009 at 4:51 PM 
First of all, thank you for the toolkit, it's really good introduction to MVVM principles!
How would you go about connecting to SQL server database(using LINQ) as dataprovider in Models? I've tried adding a dbml file to the Models directory but I'm stuck with instantiating DataContext (where should this be done) and retrieving data from the DB. Any guidance/links to helpful blogs/posts would be much appreciated!

investor5555 Jul 28, 2009 at 8:11 AM 
I cannot seem to create a project.
Error: This template attempted to load an untrusted component. Microsoft.VisualStudio.Presentation.Extensions, Version 9.0.0.0, etc... see documentation on Customizing Project Templates.
Well, WHERE??? HOW???

WPFUser Jul 24, 2009 at 10:31 AM 
The project template doesn't work for me. I have Visual Studio 2008, (german Version) I installed it but i can't see the project template (yes, I have marked .NET framework 3.5, and i have 3.5 SP1).
What can we do ?

PatrickDanino Jul 23, 2009 at 11:46 PM 
@RogerFeeley: The WPF Toolkit and the WPF M-V-VM Toolkit are two separate product releases. In order to get the M-V-VM project in VS 2008, you would need to download and install the latter from the WPF Futures page (currently at http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14962).

RogerFeeley Jul 17, 2009 at 2:04 PM 
I have downloaded the WPF Toolkit but I'm not sure it's the right one. To see if the MVVM is properly installed, I create a new project and click on 'Windows' under the C# folder. I should see 'WPF Model-View Application' but I don't. I take this to mean that I haven't installed the right thing. The links keep taking me to WPFToolkit.msi. It seems to me that there should be a WPF M-V-VM Toolkit.msi somewhere.

Here's the info from Help/About in my VS2008.


Am I crazy?

Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1

Installed Edition: Professional

Microsoft Silverlight Projects 2008 91605-141-0231645-60217
Microsoft Silverlight Projects 2008
Version 9.0.30730.126

Microsoft Visual Basic 2008 91605-141-0231645-60217
Microsoft Visual Basic 2008

Microsoft Visual C# 2008 91605-141-0231645-60217
Microsoft Visual C# 2008

Microsoft Visual C++ 2008 91605-141-0231645-60217
Microsoft Visual C++ 2008

Microsoft Visual Studio 2008 Tools for Office 91605-141-0231645-60217
Microsoft Visual Studio 2008 Tools for Office

Microsoft Visual Web Developer 2008 91605-141-0231645-60217
Microsoft Visual Web Developer 2008

Crystal Reports AAJ60-G0MSA4K-68000CF
Crystal Reports Basic for Visual Studio 2008


Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB944899) KB944899
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/944899.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB945282) KB945282
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/945282.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB946040) KB946040
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/946040.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB946308) KB946308
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/946308.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB946344) KB946344
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/946344.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB946581) KB946581
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/946581.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB947171) KB947171
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947171.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB947173) KB947173
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947173.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB947180) KB947180
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947180.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB947540) KB947540
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947540.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB947789) KB947789
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947789.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB948127) KB948127
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/948127.

Hotfix for Microsoft Visual Studio 2008 Professional Edition - ENU (KB953256) KB953256
This hotfix is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this hotfix will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/953256.

Microsoft Visual Studio 2008 Professional Edition - ENU Service Pack 1 (KB945140) KB945140
This service pack is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this service pack will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/945140.

Microsoft Visual Studio 2008 Professional Edition - ENU Service Pack 1 (KB947888) KB947888
This service pack is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this service pack will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/947888.

Update for Microsoft Visual Studio 2008 Professional Edition - ENU (KB956453) KB956453
This update is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this update will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/956453.

Update for Microsoft Visual Studio 2008 Professional Edition - ENU (KB967143) KB967143
This update is for Microsoft Visual Studio 2008 Professional Edition - ENU.
If you later install a more recent service pack, this update will be uninstalled automatically.
For more information, visit http://support.microsoft.com/kb/967143.

PatrickDanino Jun 25, 2009 at 8:50 PM 
@timhaynes:
Having a VB.Net version of the template (and samples) is definitely on the must-do list! It's great to hear you have it working, and I'll follow up with you directly. Thanks!

timhaynes Jun 25, 2009 at 3:09 PM 
@PatrickDanino

Thanks! I thought I was losing it. Glad to know that I'm understanding things properly.

I am going to be using the sample project (with the ViewModel *not* interacting directly with the DAL!) as a springboard to a MVVM project at work. Unfortunately, my employer requires all projects to be done in VB.NET. C# is expressly NOT allowed. This was a drag because the MVVM Toolkit only has a C# project.

So, I went ahead and took the output of the C# project and converted it to VB.NET, and got it running. At least, the Exit command definitely works, so I assume I have it all wired up properly (although the CommandReference.cs and DelegateCommand.cs files were a bit of a challenge to transliterate into VB.NET parlance!).

Is there someway for me to share this VB.NET version of the basic project with you guys, so nobody has to go through the same pain?

PatrickDanino Jun 24, 2009 at 7:54 PM 
@timhaynes:
That's a great observation, and I'll correct that in an upcoming version of the toolkit. In order to better layer the application (as simple as it is), it would have been preferable for the ViewModel to interact with the Model, and have the Model interact with any data access technology, so that should said technology change, only the Model would be potentially affected.

Thanks for the feedback!

timhaynes Jun 23, 2009 at 2:50 PM 
Thanks for publishing this toolkit. It has really helped me cut down my ramp-up time on WPF and MVVM.

I have one question, which is probably because of my inexperience with this pattern. Why is the MainViewModel.LoadContacts method aware of the ContactsDataSource class? I thought that the point of the ViewModel was to bind the UI to the Model. Although this certainly happens via the MainViewModel.LoadContacts class, I was expecting LoadContacts to talk to the Model only. The Model would then interact with the ContactsDataSource and pass the results up to the MainViewModel.

Am I wrong about the ViewModel? Is it allowed to interact directly with the data access layer??

Many thanks for any help.

lbugnion Jun 15, 2009 at 7:24 AM 
@David One issue with events instead of commands is that you cannot place events in a template (control template, data template) and then move the template to an external resource dictionary. The compiler will look for the event handler, won't find it (because resource dictionaries don't have code behind) and will break. Commands are more loose (looser?) and do not present the issues that events and their tight link create.

RelayCommand/DelegateCommand is just a simplification of the ICommand concept, encapsulating the complexity.

ivom Jun 3, 2009 at 7:24 PM 
@xuanvu:
We will try to get this out within the next 2-3 weeks. In the mean time, if you are blocked, feel free to send me (ivom at microsoft.com) an email and I will help.

@StanislavX:
Good catch. Thanks! I have added this to our backlog for the next release.

StanislavX Jun 2, 2009 at 9:13 PM 
There is no MVVM template in the localized version of Visual Studio 2008 SP1 after successful template installation. English version is ok.

xuanvu May 26, 2009 at 11:23 PM 
Do you have a rough estimate on when will the revised walkthrough/guide released? I've tried to follow it step by step but got stuck (forgot where, but I think most people here who followed it step by step has the same problem) and since I'm pretty new to WPF/MVVM stuffs, I couldn't move any further.

Thanks a lot.

PatrickDanino May 21, 2009 at 3:48 AM 
@David:
Although there is some debate about this in the M-V-VM community, part of the goal we had with the toolkit was to ensure business logic be kept in the view model, requiring as little code-behind as possible, and leveraging declarative XAML as much as possible.

Generally speaking, there is nothing wrong per se with using events, but one of the key issues with event handlers is that business logic frequently creeps its way in them (I've seen it happen!). In addition, when testing the view model logic, you then have to assume the method you think is being called by the view to perform an action is in fact the one (and only one) being called.

Although you don't get the benefit of compile time checking, by using DelegateCommand/RelayCommand, you ensure that the logic is kept in the ViewModel and avoid any doubt. Your point about compile time checks is a good one though. In fact, the same issue affects data binding in WPF as a whole, and we're aware that debugging can be painful.

I hope that helps clarify!

davidjjon May 20, 2009 at 4:49 PM 
Help! I don't get it.

I believe that I understand and appreciate most of the MVVM feature with the exception of commanding (using ICommand - RelayCommand, DelegateCommand, etc.).
I understand that the VM should not be aware of the View - makes good sense for testability; however, I don't understand the need for using the RelayCommand or DelegateCommand.
I understand that many purist insist on no code in the View's code behind; however, I am a minimalist and a pragmatist so it's okay :-)

For the sake of this discussion, let's ignore CanExecute.

So why do we need ICommand? Since the View can have its DataContext set to the respective VM object why can we not simply implement an event handler in the View code behind and use the DataContext to call a command handler directly in the VM with a single line of code? What do I gain by adding the complexity of a RelayCommand or DelegateCommand (ignoring CanExecute)?

The VM command handler can do just about anything needed on the View by just changing VM properties that the View has bound - the VM does not need and should not have a reference to the View.

By using an event handler in the code behind I gain compile time checking of the control's event name string attribute - I don't believe that the RelayCommand or DelegateCommand provide compile time errors for mistakes in a controls command string attribute.

In short, a single line direct call from an event handler in the View's code behind:
- takes significantly less code to implement
- gives compile time checking

I suppose that some may say that the View is now aware of the VM; however, it already is if it is using data binding to the VM.
I am sure that there is something that I don't understand so a short tutorial or reference on what I am missing would be appreciated.

Thanks for the MVVM toolkit - it is appreciated.
David Roh

PatrickDanino May 19, 2009 at 10:34 PM 
@danutzp:
We definitely hear you with respect to this being a start, but we felt releasing incrementally and getting feedback like yours so as to focus on the right topics for the next release, was preferable to working in the dark and releasing something that could end up completely off-base. Having said that, we will be addressing the window management issue more explicitly in an upcoming release.

In the meantime, we included a more complex sample (The Messenger sample) which shows a way (note: not the only way) of dealing with more than one window without moving too far away from the principles of M-V-VM.

PatrickDanino May 19, 2009 at 10:19 PM 
@Foxandxss:
Thanks, that helps! I'll try to get this to reproduce so we can address the issue.

danutzp May 19, 2009 at 3:19 PM 
@ivom:
The walkthrough you guys provide should be expanded to include the cases when data binding does not work. For example if the user selects (or double clicks) an item in the list box then a second window pops up and gets populated with the data corresponding to the selected item in the list box.

You cannot data bind SelectedIndex of SelectedItem to a command so how do you glue this to the ViewModel? Some code behind in the View that forwards it to the ViewModel or you bind to a property of the ViewModel?

Part 1 of the walkthrough shows the UI for this behavior on page 3, Figure 1. What would be the MVVM way of implementing this behavior?

I think your walkthrough is great for a beginner who wants to learn what MVVM is and how it is implemented but for more complex scenarios I would like to propose a part 3 of the walkthrough that shows cases where data binding and commands do not work.

Foxandxss May 19, 2009 at 2:41 PM 
@PatrickDanino. Yes, I know but it didn't works. I installed other templates like silverlight o asp.net mvc and its work.
I have to do a little hacking with your template (sorry about that).
I have VS2008 with C# support enabled. My version is: 9.0.30729.1 SP (3.5 SP1). Spanish version.

Otoh, I'm the only one that the messenger crash when i Log in? (it didn't show the contacts)

ivom May 19, 2009 at 6:59 AM 
@geophil:
Good feedback! We can probably provide support for NUNIT (and xUnit) in the next revision of the template.

@bashton:
Thanks for taking the time to do that. Please send us (wpftbest@microsoft.com) the revised version of the document and we will factor in the feedback in the next revision of the toolkit. Any feedback is greatly appreciated!

@usrini:
We have not tried building an XBAP application, but there aren't any limitations in the template-generated code we are aware of. We will take a look at this and get back to you.

@danutzp:
Re your first question:
MVVM is a design pattern and as such it does not dictate specific implementation details (although opinions on that vary ). Most WPF applications can be adapted to follow the pattern fairly closely. MVVM makes use of data-binding and commanding, but in reality data-binding is really just WPF-esque adaptation of the “observer” pattern – it’s a “off-the-shelf” facility provided by WPF to facilitate a common need, which you can reimplement with other means (we do not suggest that – but it is possible if your app has specific needs). Really, the meta point of this toolkit is to direct folks to build applications with model-view separation, as a proven best practice.

Re second question:
Acropolis was a much broader effort -- a lot of its ideas did make their way into WPF/Silverlight.
As far as MVVM tooling is concerned -- we already have a fairly solid tooling story in Cider (that is getting a bunch of additional facilities in Dev10), so one can easily imagine a simple straightforward tooling experience for MVVM applications once we clarify the usage patterns and workflows based on your feedback (please keep the feedback flowing -- it's very valuable!) I am convinced that we can deliver a very simple (simple is good!) straightforward tooling experience, by combining targeted platform and tool modifications.

danutzp May 19, 2009 at 5:08 AM 
>ivom wrote May 5 at 11:53 AM:
>Imagine a tooling experience where you can easily hookup
>elements of your UI to commands or methods on your viewmodels.
>This is something that we may consider implementing in the future,
>once we clarify the specific needs.

Ivo, wasn't this what Acropolis tried to do (and failed)?

danutzp May 19, 2009 at 5:05 AM 
What happens if the MVVM does not address all the needs of my application? For example what happens if not everything can be data bound, or if I cannot use commands?

PatrickDanino May 18, 2009 at 11:14 PM 
@ml_black:
Would you remember at what point in the walkthrough does the problem happen? The two really should match, so I'd like to make sure the walkthrough is correct and spare everyone the trouble of having to work around it! Thanks for taking the time to use the toolkit and template!

PatrickDanino May 18, 2009 at 11:07 PM 
@Number8:
First step would be to make sure the data is being loaded from the data source (XML file) correctly; you can do so by putting a breakpoint in the OnStartup method (Step 15) to ensure it's being loaded. The second step would be to check the XAML file and ensure the binding matches the property name in the ViewModel (step 18). If you run a WPF application in debug mode, you can see information about binding issues in the Output window of VS. Hope that helps!

PatrickDanino May 18, 2009 at 11:01 PM 
@Foxandxss and others:
You should only need to install the MSI to get the template to work in VS 2008, assuming you kept C# support enabled when installing the product. If you could provide us with the version of VS (as indicated in Help -> About menu), that would also be helpful for us. Thanks!

usrini May 18, 2009 at 8:55 AM 
Very good effort to make MVVM pattern learning easy. Simple sample to follow and it worked for me. Now, I am trying to change and extend the sample into an XBAP application. So, I tried to take the code in App.xaml.cs into the first page constructor. It did not work. Has any made similar attempt to apply this patten to XBAP application?

Foxandxss May 16, 2009 at 12:57 PM 
@ivom, I didn't touch the $if$ thing. I extracted the files on the .msi, installed the msi (because I don't know where to put the .dll file), and copied the two .zip files to the Project Templates folders on My Documents.

Number8 May 15, 2009 at 1:42 PM 
I am having trouble getting the sample app to run correctly. At step 19, the app runs but doesn't display the data.
Any hints on how to debug this?
TIA.

bashton May 14, 2009 at 10:35 PM 
I have a commented revision of the "Model-View WPF Applications--Part 2, Walkthrough.docx" that contains comments on the minor corrections that I found were needed to successfully build the app from scratch. Is there somewhere to send it to the team so that you can confirm my comments and refine the document?

geophil May 14, 2009 at 8:22 PM 
Very nice simple template and sample. Are there any plans to support NUnit? It would be nice to choose NUnit as the test framework

ml_black May 14, 2009 at 3:43 PM 
Hi. amdys is right. I mean that I was building the app from scratch, according to the directions in the "Model-View WPF Applications--Part 2, Walkthrough.docx". I can get the sample code to build without problem, but the directions in the document are not correct.

However, thanks for the sample app--it's been amazingly helpful.

ivom May 13, 2009 at 5:02 PM 
@Foxandxss
@SailTsao
What $if$ are you guys referring to? Also what version of VS are you using? Are you guys also unblocking the downloaded ZIP file?
Thanks

Foxandxss May 12, 2009 at 10:37 PM 
Well, installing the toolkit (for the dll), and copying the two zip to the templates folder of VS (in My Documents), it works. OTOH, the messenger example didn't work. It connect, the debugger window show the contacts but it crash with a TargetInvocationException and the debug windows says InvalidCastException. No lines or something like that. If you need more information, tell me.

Foxandxss May 12, 2009 at 9:56 PM 
@SailTsao, nothing. I extract the .msi, I extract the project zip, delete the $if$ statement with it lines, compress the file again, install but nothing. It doesn't show the project template. I think that is problem of the template itself, but not the csproj. Anyway, thank you.

SailTsao May 12, 2009 at 4:22 PM 
@Foxandxss
It doesn't work for me too.And I have Visual studio 2008 too.But after I Extract the setup file And modify the WpfModelViewApplication.csproj file,it's work for me.I delete the $if$ statement.

Foxandxss May 12, 2009 at 2:50 PM 
The project template doesn't work for me. I have Visual Studio 2008, I installed it but i can't see the project template (yes, I have marked .NET framework 3.5, and i have 3.5 SP1). I have the silverlight toolkit and it works, even asp.net mvc template.

I will keep trying.

amdys May 8, 2009 at 1:27 AM 
Ivom :
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

I am with you. just starting learn wpf less than 3 months. Using this toolkit today help me alot understand it better. Maybe i will better grap that prism next time. Not much time for now frankly...so keep it up guys. cant wait for next version.

I think what ml_black means that if you follow the walkthrough step by step like a beginner like me. Its really not working thats why he and me need to look at finished working app which yes working fine. ex step 9. It will raise error.

Thanks for this toolkit.

amdys May 8, 2009 at 1:24 AM 
Ivom :
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

I am with you. just starting learn wpf less than 3 months. Using this toolkit today help me alot understand it better. Maybe i will better grap that prism next time. Not much time for now frankly...so keep it up guys. cant wait for next version.

I think what ml_black means that if you follow the walkthrough step by step like a beginner like me. Its really not working thats why he and me need to look at finished working app which yes working fine. ex step 9. It will raise error.

Thanks for this toolkit.

amdys May 7, 2009 at 10:45 AM 
Ivom :
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

I am with you. just starting learn wpf less than 3 months. Using this toolkit today help me alot understand it better. Maybe i will better grap that prism next time. Not much time for now frankly...so keep it up guys. cant wait for next version.

I think what ml_black means that if you follow the walkthrough step by step like a beginner like me. Its really not working thats why he and me need to look at finished working app which yes working fine. ex step 9. It will raise error.

Thanks for this toolkit.

amdys May 7, 2009 at 9:37 AM 
Ivom :
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

I am with you. just starting learn wpf less than 3 months. Using this toolkit today help me alot understand it better. Maybe i will better grap that prism next time. Not much time for now frankly...so keep it up guys. cant wait for next version.

I think what ml_black means that if you follow the walkthrough step by step like a beginner like me. Its really not working thats why he and me need to look at finished working app which yes working fine. ex step 9. It will raise error.

Thanks for this toolkit.

amdys May 7, 2009 at 9:09 AM 
Ivom :
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

I am with you. just starting learn wpf less than 3 months. Using this toolkit today help me alot understand it better. Maybe i will better grap that prism next time. Not much time for now frankly...so keep it up guys. cant wait for next version.

I think what ml_black means that if you follow the walkthrough step by step like a beginner like me. Its really not working thats why he and me need to look at finished working app which yes working fine. ex step 9. It will raise error.

Thanks for this toolkit.

ivom May 6, 2009 at 7:00 PM 
Interesting -- I just tried this again and the ContactBook sample does build cleanly.
What SKU of VS do you have? Also, have you "unblock"-ed the ZIP before unzipping?
Thanks

ml_black May 6, 2009 at 1:02 PM 
I meant the ContactBook app that you can build from scratch using the guide in the documentation. I'll have a look at the completed solution to see what might be different from what's in the doc.

ivom May 6, 2009 at 11:51 AM 
@ml_black:
Do you mean the Messenger Sample app? That builds fine on VS 2008 -- I just confirmed that again downloading a fresh copy of the ZIP file. Note that after you download the ZIP you may need to "unblock" it (right-click on it > Properties > Unblock)

ml_black May 5, 2009 at 9:03 PM 
Has anyone actually tried to build the sample app in this toolkit? There are lots of errors in the code. I've fixed the ones I've encountered so far, but where I'm not so familiar with delegates, I'm having trouble getting around the problem of converting the boolean returned by the private CanClearContactBook method into the System.Func<bool> required by the delegate command.

ivom May 5, 2009 at 5:53 PM 
@jonathanyates
@smatusan

First of all, thanks for your feedback and for taking the time to provide it. It is much appreciated!

There are three reasons why we released the "WPF MVVM Toolkit" in its present form:
a) Provide simple MVVM ramp-up guidance to WPF developers
b) Demonstrate general direction and solicit feedback from the WPF / MVVM community
c) Identify and correct limitations in the WPF platform
d) Test and prototype tooling experiences for MVVM applications

Re (a):
The default WPF template in Visual Studio, while great for lowering the entry barrier to WPF, is not suited for any sufficiently complex application. PRISM on the other hand is great, but it is a fairly complex solution that is not easy to grasp. Besides, it deals with a slightly different problem (composable UI). There have been a lot of great articles on MVVM, but we have also seen a fair number of people struggling with finding the single definitive article and ending up doing the wrong thing. That is why we decided to release the toolkit and that’s why we decided to keep it simple and “easily graspable”. What we want to achieve is “ease the entry into the MVVM domain”, not provide a definitive, all-encompassing guide – at least not in v.0.1.

Naturally, if you are already well-versed in PRISM and/or MVVM, you don’t need this toolkit. If you, however, are trying to pick up WPF or trying to figure out how to achieve model-view separation in your WPF application, we suggest you read through the guidance and the samples .

Re (b):
We know that there is a lot of passion and expertise around MVVM out there. We invite everybody who is passionate about this space to collaborate with us to come up with a simple, easily-graspable, complete MVVM solution that we can then integrate in Visual Studio. So please, send us your feedback.

Re (c):
Even though you can obviously build WPF MVVM applications today, there are certain limitations in WPF that force you to do “unnatural acts” in order to accomplish proper model-view separation. An example of such limitation is the fact that you cannot bind a routed command to a viewmodel (which is typically not part of the element tree) – this is worked-around by solutions such as DelegateCommand / RelayCommand / Etc. Another limitation is the fact that you cannot bind input gestures to commands on the viewmodel (because the Command, CommandTarget and CommandParameters properties on InputBinding are not DPs) – we are considering fixing this in WPF 4.0.

Re (d):
Imagine a tooling experience where you can easily hookup elements of your UI to commands or methods on your viewmodels. This is something that we may consider implementing in the future, once we clarify the specific needs.

So I invite you to work with us and help us deliver a simple, natural, componentized, toolable MVVM solution. A lot of you have very valuable real world application experience that we’d like to tap into.

Ivo

smatusan May 5, 2009 at 8:08 AM 
I agree with jonathanyates.
WPF Model-View-ViewModel Toolkit should be somehow integrated with the prism. It seems that, toolkit looks like smaller subset of the prism.

jonathanyates May 4, 2009 at 11:22 PM 
This is yet more confusion to us all. The whole MVVM paradigm for WPF is now getting way out of control. Trying to decide on the best approach I am torn between so many differing approaches regarding WPF. I hope I’m not alone here. Here’s my gripe:

1. Firstly we’ve got Prism with its guidance using PM and MVVM

2. Then Josh Smith’s excellent article talks about using Data Templates to render the VM’s view.
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

3. Next up Julian Dominguez’s blog and subsequent Spike shows us how to implement Josh’s pattern using Prism.
http://blogs.southworks.net/jdominguez/2008/09/presentation-model-with-datatemplates-in-compositewpf-prism-sample/

4. And now we have this MVVM toolkit which uses neither of Josh’s or Julian’s approaches. In fact it offers little other than a template project with a few folders.

5. And now I here of something called ‘Caliburn’.
http://caliburn.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=23840

STOP!!!! Where will it all end.

There currently seems to be a million and one ways in which people are coming up with best practices for Model-View separation, with no clear standard. Currently I’m using Prism with a vaiation of MV described in Julian Dominguez’s blog. Please, please, please can someone offer some kind of ‘Guidance’ as to the best practices we should all be using for WPF. After all, isn’t that what CodePlex and P&P is all about.

ivom May 4, 2009 at 4:43 PM 
@ralexand56:
Valid feedback! We will address this in our next revision.

ralexand56 May 3, 2009 at 8:10 PM 
Boooooo!

Where is the visual basic version. We're still out here. Not all wpf developers use C#.

shaggygi May 2, 2009 at 10:28 PM 
Thanks for reply. I'm confused about the RIA Services. I thought RIA Services allow to easily retrieve data ( Mainly for SL apps ). Brad mentioned RIA Services are on the roadmap for WPF. Do you know when we will get Bits that support WPF? Anyway, could you explain more about similar or differences of this M-V-VM toolkit and RIA Services? Is one based on WPF and the other for SL? Can they be used together? Thanks again.

PatrickDanino May 2, 2009 at 6:47 AM 
Thanks for the kind words and the great questions. Part of the goal in releasing the toolkit early is to gather feedback, which will directly impact the plans we have for it, including getting the template in VS 2010. With respect to Silverlight, there is an ongoing effort by Brad Adam's team to develop rich internet application services, and they currently have a toolkit of their own (see http://code.msdn.microsoft.com/RiaServices).

MEF provides a lot of functionality, much more than is currently included in this toolkit. Learning how to take advantage of all those features does require more time though, so our hope with this early release is that a person who is not very experienced with M-V-VM could start with this toolkit, get familiar with the pattern, and then make use of MEF (or other M-V-VM frameworks) based on the complexity and requirements of the scenario. In addition, part of the feedback we get will help decide if we will develop as part of the template a more complete M-V-VM framework, use an existing one, or keep things as-is and let each person use their preferred framework. Hope that helps clarify!

shaggygi May 2, 2009 at 1:05 AM 
Also, does this topic fit into the new MEF? If so, do you have any examples on how to combine the two? Thanks again.

shaggygi May 2, 2009 at 12:54 AM 
Great stuff. This has good details on topic and got me running in no time. Is there plans on releasing something like this for Silverlight? Will the template be a default project type in VS 2010? Thanks.