It is a common scenario to navigate to another URI on a button click. If you have MVVM fully wired up, this command in your View will likely connect to a method in your ViewModel. But the View is the one that can navigate via the NavigationService.

So, this means the ViewModel will need to tell the View to do so. One way to do this is by using the Messaging framework in MVVM Light toolkit.

For e.g. Let’s say your About button needs to go to to the About Uri. The following are the various steps involved.

1. View – Register the Message (to receive) in the source View.
Let’s say we want to look for a Uri and a token of “Navigate”. When it is received, we want to invoke the Navigation. This can be elegantly done with a delegate as follows

Messenger.Default.Register<Uri>(this, "Navigate", 
        (uri) => NavigationService.Navigate(uri));

2. ViewModel – Send this Message from a ViewModel, and MVVM Light will deliver it to the View

private void About(object param)
    Uri uri = new Uri("/View/About.xaml", UriKind.Relative);
    Messenger.Default.Send<Uri>(uri, "Navigate"); 

This ensures there is no tight coupling between the ViewModel and the View, as the pattern requires.

You can use this pattern even between 2 Views if you need.


One issue I came across while using a ListBox with a DataTemplate, instead of a DataGrid was a compiler/xaml error stating that the Items collection should be empty…

This was a little baffling and I was wondering if there were data-binding problems in the code-behind, etc. Turns out the source of the problem was in the XAML.

I had:

<ListBox x:Name=”foo”>

and I needed to add in a ItemTemplate container tag, like so:

<ListBox x:Name=”foo”>

I think the error was happening because the framework was considering the DataTemplate as an item in the list, and hence the Items collection was not empty and messed with the binding.
Adding the ItemTemplate tag specified it as a template, and not a list-item