Search Wiki:
Article Description
Here I explain just how simple it can be to build a WPF application the right way using the MVVM Pattern.


This article is available at: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
Last edited Jan 25 2009 at 3:33 PM  by howarddierking, version 3
Comments
riles01 wrote  Jan 29 2009 at 5:23 AM  
Josh, this was awesome. Thanks for the great article and for the clear and sound guidance on wrapping the model. I've seen (and used) a "ViewModel" that hosted the model and referenced the view--a clear case of a presenter. Ah well. :)

siegelem wrote  Feb 12 2009 at 6:15 AM  
Just had my ah-hah moment. CustomerType property in CustomerViewModel opened the eyes finally.

Thanks for the contributions!

stevensrf wrote  Feb 20 2009 at 5:22 PM  
Great article
Can someone explain what the syntax
param =>
means? I am not quite sure what the param variable is and where it is defined at.
The msdn library did not show param as an attribute.
And the operator => where does come from.
I do not see no IEnumerate interface involved here.
If anyone can reference me to a website to explain param and the => operator
I would greatly appreciate it or you can email me at
sun_Water_snow@hotmail.com and explain this to me.

RelayCommand _saveCommand;
public ICommand SaveCommand
{
get
{
if (_saveCommand == null)
{
_saveCommand = new RelayCommand(param => this.Save(),
param => this.CanSave );
}
return _saveCommand;
}

dfmartin72 wrote  Feb 23 2009 at 8:54 PM  
Steven: The "param =>" syntax is a Lambda extression which is an anonymous function. The RelayCommand object takes an Action<T> as a parameter which is a concrete delegate. By passing "param => this.Save()" you are essentially passing a delegate to the Save method into the constructor of the SaveCommand. For more information on Lambda check out: http://msdn.microsoft.com/en-us/library/bb397687.aspx

dfmartin72 wrote  Feb 23 2009 at 8:56 PM  
Should really check my spelling *before* I add the comment -- "extression" should be "expression". Disclaimer: other misspellings may exist since the comment box lacks red squiggly lines. :)

branck wrote  Mar 1 2009 at 3:37 PM  
Josh,

When I went to the download tab I was pleasantly surprised to find the VB Version already converted. (I was kind of dreading it because I knew you used a lambda expression and figuring out the syntax was going to be yet another hassel). So Thanks! Gee, are you actully going to let us VB guys set at the same table or was this just for Karl?

Bob Ranck

digger69 wrote  Mar 26 2009 at 8:32 PM  
It is not clear to me how changes in the model (Customer) are bubbled up to the ViewModel since Customer doesn't implement INontifyPropertyChanged? So, if something happened in the Model that updated one of its properties, the viewmodel wouldn't be notified (Say my biz rul was when City is set to Pittsburgh, then FootballFan is set to True if not already). What am I missing?
Side note: Seems VB code zip has extra files under TestResults bloating the zip.

juanagui wrote  Mar 31 2009 at 8:57 AM  
I've just created a discussion on this excellent article:

Any ideas on how to provide mock data to the views hence improving the design experience with blend?

My goal is to use implicit views (ie, setting the data template for the viewmodel in a resource dictionary as described in the article) and have data in Blend's design surface without additional code behind in the view.

So far I've tried this http://blogs.msdn.com/jaimer/archive/2009/02/20/design-time-data-in-blend.aspx and this http://mikestedman.blogspot.com/2008/11/objectdataprovider.html but didn't get anywhere.

Thoughts?

tareldo wrote  Apr 15 2009 at 7:43 AM  
The article is great but I think that amount of extra code can be eliminated by applying attrubutes to methods without manual definition of RelayCommand instances eg

public class CustomerViewModel {
[SomeCommandAttribute]
public void Save()
{
}
[SomePredicateCommandAttribute]
public bool Save()
{
}

private object _commands;
//virtual class we will bind to
public object Commands{
get {
if (_commands == null)
{
_commands = StubClassFactory<CustomerViewModel>.CreateStub(this);
}

return _commands;
}

}
As for WPF binding you can create a factory class that will dynamically generate a stub class with ICommand properties
via IL and bind wpf to this stub class changing binding path to someth like "Commands.Save" .

tareldo wrote  Apr 15 2009 at 8:28 AM  
* erratum
[SomePredicateCommandAttribute]
public bool CanSave()
{
}

sudhir75 wrote  Apr 20 2009 at 4:40 AM  
I can't get Expression Blend 2 to open the solution successfully. Throws an exception:

jpmv wrote  Jun 3 2009 at 2:49 PM  
Hello everyone,

I've been studying the VMMV pattern to understand it's usefullness in a project i'm currently in, and i'm very confused wether this is the best approach

The project architecture is the following:

1) Presentation Layer: XBAP
2) Business/Application Server: IIS+WCF
3) Storage: Oracle 10

- communication between the 3-tier will be solely XML
- 90% of the forms on XBAP will be dynamicaly built from the oracle's metadata (probably some user controls to wrap some things up)


This is an existing application with a huge oracle database, which contains all metadata that will generate hundreds of forms *dinamically* in runtime.

My question is:

1. On this scneario the MVVM pattern still applies?
2. How would the VIEW part of MVVM be built if its dynamic?

Best regards,
jpmv

sskassam wrote  Aug 20 2009 at 2:12 PM  
Hey where did the article go? It shows as unavailable now, and I was just using it yesterday. Any clues?

Zest4Quest wrote  Sep 14 2009 at 2:18 PM  
Hi,
If i understood correctly, none of the viewModel classes would not have any referrence to UI namespaces like Windows.Forms.If so, how do we show a pop up form in the MVVM model? I mean where do we instantiate an instance of a Form or Dialog box and call the Show method if we are trying to not add any code behind the xaml file? I am wondering where i should add the code to show a Form as a Modal dialog box in the MVVM pattern..
Thanks in advance..
Zest4Quest

dvarrin wrote  Nov 27 2009 at 9:41 AM  
Hi,

In the CustomerViewModel, there is a "CustomerType" property which is able to have an unknown value when it is null. Then before we can create a new customer, we have to check if this property is set to Company or Person.

Would it be possible to use a 'boolean?' property in the datamodel, which can have the values true, false or unknow?

Or it is not possible because a 'boolean?' means that the value can be empty in the database?

Best regards,
Daniel

dcveeneman wrote  Apr 6 2010 at 2:38 PM  
I just came back to this article after about a year. I am very impressed with it. Well done, and thanks.

warez_willy wrote  Jun 16 2010 at 11:01 AM  
CAn I ask where the Entity model is in this project (Edmx) or where I would put it

EvilPigeon wrote  Jul 11 2011 at 7:16 AM  
For anyone having trouble downloading the source files: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&DownloadId=4357 (C#) and http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mag200902MVVM&DownloadId=4499 (VB.Net)

Updating...
Page view tracker