Posts
417
Comments
233
Trackbacks
42
Practical Window Phone 7 ListBox Usage

Almost every example of a Silverlight ListBox that I have found merely shows how to get data bound to it, not what to do once you get it there.  What good is a ListBox that you can’t get a reference back from?

The quickest practical way is to hold your original datasource and use the index of the selected item as you key back into your data.  It isn’t pretty and you wouldn’t want to take this approach with large amounts of data, but then again a mobile device isn’t meant to display large amounts of data to begin with.  Here is the sample of the code to retrieve an object from a List.

if (listBox.SelectedIndex > -1)
{
var tempObject = _myObjectList[listBox.SelectedIndex];
// Do something important like delete the object
// or navigate to another page using the object data
}

Of course there are other options, especially if this is a navigation scenario.  You could use a ListBoxItem template that would contain a HyperlinkButton and bind the identifier for your object into the URI query string.  I am still looking for other more elegant solutions but these options can help get your application moving.

posted on Monday, August 15, 2011 9:48 AM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Comment *  
Verification

Tim Murphy

Tim is a Solutions Architect for PSC Group, LLC. He has been an IT consultant since 1999 specializing in Microsoft technologies. Along with running the Chicago Information Technology Architects Group and speaking on Microsoft and architecture topics he was also contributing author on "The Definitive Guide to the Microsoft Enterprise Library".



I review for the O'Reilly Blogger Review Program



Technorati Profile

www.flickr.com
Tag Cloud