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.
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.
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.