MVVM Project and Item Templates

Posted by Timmy Kokke on Geeks with Blogs See other posts from Geeks with Blogs or by Timmy Kokke
Published on Wed, 16 Jun 2010 06:29:30 GMT Indexed on 2010/06/16 6:42 UTC
Read the original article Hit count: 372

Filed under:

Intro

This is the first in a series of small articles about what is new in Silverlight 4 and Expression Blend 4. The series is build around a open source demo application SilverAmp which is available on http://SilverAmp.CodePlex.com.

 

MVVM Project and Item Templates

Expression Blend has got a new project template to get started with a Model-View-ViewModel project  easily. The template provides you with a View and a ViewModel bound together. It also adds the ViewModel to the SampleData of your project. It is available for both Silverlight and Wpf.

clip_image002To get going, start a new project in Expression Blend and select Silverlight DataBound Application from the Silverlight project type. In this case I named the project DemoTest1.

clip_image004

The solution now contains several folders:

  • SampleData; which contains a data to show in Blend
  • ViewModels; starts with one file, MainViewModel.cs
  • Views; containing MainView.xaml with codebehind used for binding with the MainViewModel class.
  • and your regular App.xaml and MainPage.xaml

The MainViewModel class contains a sample property and a sample method. Both the property and the method are used in the MainView control. The MainView control is a regular UserControl and is placed in the MainPage.

You can continue on building your applicaition by adding your own properties and methods to the ViewModel and adding controls to the View.

Adding Views with ViewModels is very easy too. The guys at Microsoft where nice enough to add a new Item template too: a UserControl with ViewModel. If you add this new item to the root of your solution it will add the .xaml file to the views folder and a .cs file to the ViewModels folder.

image

Conclusion

The databound Application project type is a great to get your MVVM based project started. It also functions a great source of information about how to connect it all together.

 

© Geeks with Blogs or respective owner