The MVVM (Model View View Model) design pattern is a popular way to create WFP (or Silverlight or Windows Phone 7) applications.Despite the obscure name, the design pattern is surprisingly simple. Basically, the programmer creates a view model which is bound the the view (i.e. the xaml) via WFP binding model. Thanks the WPF’s two way bindings, the INotifyPropertyChanged interface and the ObservableCollection<T> there’s very little need for any other direct interactions between the view and the bound model.

I had the idea to use F# to implement the ViewModel, the object that exposes the values that xaml is bound to, when doing some work in Silverlight. To ensure a GUI remains responsive it’s important to take advantage of the asynchronous programming model when writing your view model. This means view models written in C#, or at least pre-C# 5.0, must make heavy use of continuation passing style which can make it difficult to co-ordinate many asynchronous calls. In F# we can use asynchronous workflows to tidy this up a bit. We’re going to show this by writing a small app that will connect to Yahoo! Finance download some stock data and present this in a DataGrid with a drop down list to select the company who’s stocks we’re interested in. Here’s the not very interesting GUI:

Obviously if we have a designer on the team and the blend package we can do a lot better than this.

A view model is class, which is declared in F# like this:

  1. // A view model for a simple stock price history viewing app
  2. type StockViewerViewModel() =