1 Simple Step for Commanding in Silverlight

Silverlight 4 is now supporting the commanding that we’ve come to love from WPF. Commanding was a foundational feature for MVVM. It’s what enabled us to bind to methods on our view models.

John Papa has an excellent introductory post on using commands in Silverlight 4. This pattern is called the DelegatingCommand pattern (or sometimes RelayingCommand).

I’d like to show you the Caliburn way of handling this. I’m going to steal John’s viewmodel from the aforementioned post (and hope he doesn’t sue).

The viewmodel presents a collection of all products and a filtered collection of products. You can modify the filter by calling LoadProducts().

public class ProductViewModel
    public ProductViewModel()
        Products = new ObservableCollection<Product>();

        AllProducts = new ObservableCollection<Product>
                              new Product {ProductId = 1, ProductName = "Apple"},
                              new Product {ProductId = 2, ProductName = "Orange"},
                              new Product {ProductId = 3, ProductName = "Banana"},
                              new Product {ProductId = 4, ProductName = "Pear"},
                              new Product {ProductId = 5, ProductName = "Grape"},
                              new Product {ProductId = 6, ProductName = "Grapefruit"},
                              new Product {ProductId = 7, ProductName = "Strawberry"},
                              new Product {ProductId = 8, ProductName = "Melon"},
                              new Product {ProductId = 9, ProductName = "Guava"},
                              new Product {ProductId = 10, ProductName = "Kiwi"},
                              new Product {ProductId = 11, ProductName = "Pineapple"},
                              new Product {ProductId = 12, ProductName = "Mango"}

    public ObservableCollection<Product> AllProducts { get; set; }

    public ObservableCollection<Product> Products { get; set; }

    public void LoadProducts(string filter)
        var query = from p in AllProducts
                    where p.ProductName.ToLower().StartsWith(filter.ToLower())
                    select p;
        foreach (var item in query)

    public bool CanLoadProducts
        get { return true; }

Let me point out a few notable differences between John’s original and my version of the viewmodel.

  • There is no ICommand property.
  • CanLoadProducts is a property on my model, it was a method on the original

The snippet from the corresponding view would look like this:

    <TextBox x:Name="filter" />
    <Button x:Name="LoadProducts" Content="Load" />

The Explanation

It’s binding by convention, and it is a new feature of Caliburn added after the 1.1 release[1]. So you’ll need to get the trunk to do this.

When you have a button named LoadProducts, Caliburn will look for a method on your viewmodel named LoadProducts and automagically handle the binding for you. In addition, it will check for either a method or property (as in this example) named CanLoadProducts that it will use to toggle the enable/disabled for the button. Also, since we have a parameter named “filter” on LoadProducts, Caliburn will check to see if there is an element in the view name “filter” and pull the value from that.

There’s a great deal more that you can do with conventions (and you can even plug in your own conventions).

This also works for Silverlight 3 and WPF.

You can also see an example of this in Ayende’s sample app Alexandria.

  1. Actually, you can do some of this in 1.0, but it is easier and much richer in the trunk. Ping the forums if you have a need to use it in 1.x.

Posted 03-03-2010 11:24 AM by Christopher Bennage
Filed under: , , ,


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)