MVVM (Model-View-View Model) is a programming pattern similar to MVC that you can use to decouple the model and the view from business logic. There are many articles on the internet about what MVVM is so I’m not going to talk about that. In this article I want to concentrate on the most basic MVVM application, understanding it, and how to move on from there.

This example doesn’t have a Model. More than likely when building even the simplest application you will need access to data. If you create an Entity Framework (ADO.NET Entity Data) Model, this would indeed be ...

This week our CEO, Lino Tadros, is the special guest on  the popular .NET development show, “Yet Another Podcast”. The show is hosted by Jesse Liberty, a Senior Developer-Community Evangelist on the Windows Phone Team. Jesse and Lino discuss developing applications for Windows Phone, MVVM, Silverlight, Visual Studio 11 and more. Lino also makes a big promotional offer during the interview, so if you are itching to learn a little bit about Windows Phone, you are going to want to tune in.


Grab the podcast here: http://jesseliberty.com/2012/02/06/yet-another-podcast-59lino-tadres/

The MVVM design pattern works extremely well in Silverlight.  A typical implementation of this pattern is to create a ViewModel for each View or Page in your application.  This gives you a clean separation between your View and ViewModel and it also provides logical separation for your ViewModels so they don’t get bloated.  For larger applications, you may find that each View has multiple ViewModels or a Page is comprised of several UserControls, each of which have their own ViewModel.  Once you get to this point, it can become challenging to communicate between the ViewModels and tie everything together in ...

Someone recently asked me for advice on how to bind the visual state of a Silverlight control to a property of the view-model.  Specifically, they had a Boolean property on their view-model named something like IsExpanded that they wanted to control the expanding and collapsing of a control in the view with the value of this view-model property.

It was such a fun sounding challenge—and one that seemed like it would be very useful to have for many situations—that I couldn’t resist creating a simple solution. 

Here’s What I Created (Silverlight 4 Plug-In Required)

Get Microsoft Silverlight

 

Here’s How I Did It...

Quite often in my copying and pasting frenzies for Silverlight properties that use notifications I will forget to change the string inside the constructor to PropertyChangedEventArgs(string). The string is the name of the property that needs to send the property changed event. More times than not the calling property getter or setter is indeed the property that needs to be changed so I updated my RaisePropertyChanged method to look at the call stack and find the name.

 

Here’s my NotifyPropertyBase abstract class that I base all my View Models upon:

 1:  public  abstract  class NotifyPropertyBase : INotifyPropertyChanged
 2: {...

I’m sure once you have tried to use DataTemplates in certain controls you will notice that if you give a name to an element you cannot access that element outside the DataTemplate. This makes sense because there will end up being many of these items created in a template and the name you give the element will not be unique. So how would you set an element’s property in the DataTemplate that comes from a FrameworkElement property? It can be done with Dependency Properties.

 

As a side note, the Dependency Properties in this example will work around the issue ...

I love the M-V-VM pattern for Silverlight and WPF applications.  It’s beautiful and works very well, but sometimes it is too verbose, particularly in the ViewModels’ INotifyPropertyChanged implementations.  I find that I spend too much time on the repetitive plumbing tasks that could otherwise be spent programming the actual application logic.

There are many tools and frameworks out there to speed up and simplify MVVM (Caliburn, MVVM Light, WAF, etc.), but I just discovered a very simple tool that goes a long way toward making MVVM more useable. 

It’s called NotifyPropertyWeaver and it solves the problem ...

What makes RadDomainDataSource MVVM friendly and why did Telerik build it? In his excellent blog "An MVVM Approach to Telerik Domain Services for Silverlight", Rossen Hristov says:

"I truly believe that every time someone places an UI element or a control in his view model, a baby kitten dies somewhere."

Here at Falafel, I do believe its company policy not to kill kittens and our code base must reflect this. The secret sauce that allows us to move DomainDataSource functions cleanly into the ViewModel is the QueryableDomainServiceCollectionView (QDSCV) class. RadDomainDataSource is simply a control wrapper around the QDSCV ...

Expression Blend 4 makes invoking a Command, drag-and-drop simple. Working in Expression Blend 4, I have a project with a "MyCommand.cs", "MyViewModel.cs" (both from the Using Commands and MVVM blog project) and an unbound button on the main form:

In Blend, click on the Assets tab and select the Behaviors item in the list. The list contains both "Actions" and "Behaviors".

Actions vs. Behaviors

Silverlight differentiates between "Actions" and "Behaviors". Actions use "Triggers" (e.g. events, storyboard state changes...) to initiate commands, property changes or some other outcome. For example, a mouse Click event can trigger a command execution. "Behaviors" are similar to ...

To separate business action logic from presentation in a Silverlight app, you should be using commands. The logic for "Save Document" or "Post Invoices" really has no place in the page code behind, and certainly doesn't need to show up twice when invoked from both a button and a menu. Commands encapsulate logic for some business action, e.g. "Save Document".  that can be bound declaratively in XAML. Commands also fit neatly into the MVVM pattern where the command itself lives in the ViewModel.

To create and use a command:

  • Create a class that implement the ICommand interface. 'Easy-peasy' -- ...