Derik Whittaker

Syndication

News


Handling the SelectionChanged event in WP7 using MVVM Light

If you are building out a WP7 (or just a standard Silverlight application) and you are following the MVVM design pattern you may be a left wondering how to handle the selectionChanged event in a Listbox without the use of a code behind.  For this post we are using the MVVMLight framework to accomplish our goals, but this same concept should hold true in all general cases.

Step 0 – Setting up your system

In order to get this up and running you will need to have downloaded and referenced the MVVMlight framework in your application.  You will want to reference:
GalaSoft.MvvmLight.Extras.WP7
GalaSoft.MvvmLight.WP7


Step 1 – Creating your references in your .Xaml file

The image below will show you how to setup your references in your .xaml file to have the right aliases

image

 

Step 2 – Defining your Triggers in XAML

 

image

In order to NOT have to use the code behind we need to use the interactivity triggers which are available to use in Xaml.  What you want to pay attention to here is the Eventname.  This needs to be SelectionChanged.  For the EventToCommand make sure you provide the name of the ICommand property in your ViewModel (we will create this in a minute).  Also make sure to set the PassEventArgsToCommand to True.

 

Step 3 – Defining your ICommand in your ViewModel

First lets create our property which is our ICommand (here it is implemented as a RelayCommand class)

public RelayCommand PatientTaskSelectedCommand { get; set; }

Next we want to create the pointer for our Comman

PatientTaskSelectedCommand = new RelayCommand( HandlePatientTaskSelected );

Finally our method HandlePatientTaskSelected method which does the heavy lifting

private void HandlePatientTaskSelected( SelectionChangedEventArgs args )
{
    if ( args == null || args.AddedItems.Count == 0 ) { return; }
    if ( args.AddedItems[0] as Task == null )
        { throw new InvalidCastException("The model type provided for Patient Task was not a Task, this is not correct.");}

    var task = args.AddedItems[0] as Task;

    RaiseNavigateToPage( RouteCreator.ToPatientTaskDetails( task.Id ) );
}

That is all, as you can see there is a bit of work which needs to be done, but not a tone.

Till next time,


Posted 08-05-2010 5:07 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Rob Eisenberg wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-05-2010 6:43 PM

Actually, there is an easier way, that does not require any framework, assuming you are not doing multi-select. Create a property on your VM that represents the selected item. Whenever that property is set, execute the custom code. If you were using Caliburn.Micro (wink..wink), you could use a Conductor<T> to do this ;)

Derik Whittaker wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-05-2010 6:49 PM

Rob,

Actually we have used that one as well and yes that is also an option.

Hey, can you email me your sample app for WP7 and Caliburn.Micro

Corrado Cavalli wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-06-2010 1:35 AM

Why don't you bind SelectedItem property using TwoWay mode to a SelectedItem property exposed by the ViewModel?

Rob Eisenberg wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-07-2010 7:18 PM

I'll have the sample attached to the next blog I post....which should be either today or tomorrow.

Jason Rainwater wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-10-2010 12:05 PM

This seems like alot of work when you can just bind the SelectedItem property to an exposed property on your viewmodel?

Charles Strahan wrote re: Handling the SelectionChanged event in WP7 using MVVM Light
on 08-16-2010 5:55 PM

While I agree with Bob, this is a great example for other scenarios where one needs to execute a command when a given event is raised. Thanks Derek ;).

Oleksandr Krakovetskiy blog wrote Дайджест технических материалов #5 (Windows Phone 7)
on 10-27-2010 7:32 AM

Tools, Books, Guides Windows Phone 7 Developer Tools RTM (online installer) , ISO UI Design and Interaction

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
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

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)