Over a million developers have joined DZone.

1 Simple Step for Commanding in Silverlight

DZone 's Guide to

1 Simple Step for Commanding in Silverlight

· ·
Free Resource

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.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}