Data Binding the WP7 ListPicker to XML data using ExpressionBlend 4
published on: 11/13/2010 | Views: N/A | Tags: WP7Toolkit Blend Binding ListPicker
by WindowsPhoneGeek
Displaying data is an important part of every application. This functionality is a core concept in Silverlight and Expression Blend makes it accessible to the UI designer. In this tutorial, I will demonstrate how to bind the Windows Phone 7 ListPicker to XML Data using the Expression Blend designer.
Getting Started
The first thing you will need is Expression Blend 4. You can get the it here. Once you've installed Expression Blend 4 just follow the steps:
1. Start a new WP7 application project and add reference to:
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Nov10\Bin\Microsoft.Phone.Controls.Toolkit.dll

2. Select the AssetsPanel and begin typing ListPicker, you will see two suggestions ListPicker and ListPickerItem. Drag a ListPicker Item over the design surface. 
Add Sample Data
Most of the user interfaces for typical LOB (line of business) applications are being driven by data. Controls might use data from external XML files, databases, or other data sources. In this example I will show you how easy it is to bind the ListPicker control to an external XML file.
The XML file is very simple, it contains information about cities:
<?xml version="1.0" encoding="utf-8" ?>
<Cities>
<City>
<Name>Madrid</Name>
<Country>Spain</Country>
</City>
<City>
<Name>London</Name>
<Country>UK</Country>
</City>
<City>
<Name>Las Vegas</Name>
<Country>USA</Country>
</City>
<City>
<Name>Milan</Name>
<Country>Italy</Country>
</City>
</Cities>
To begin with addin data follow the steps:
1. Make a XML file that contains the data.
2. Click the Data Pane and then click the create sample data source icon.
3. From the drop-down menu, click Import Sample Data from XML:
4.A dialog box will appear. Browse your XML file containing the data you need.
5.Give a name to the data source : CitiesDataSource.

After that Expression Blend will generate a DataSource called CitiesDataSource. Clicking the EditSampleValues icon allows you to fully customize your data by adding new properties, changing the existing ones or adding new values.
Note: You can define a new data source by selecting "Define New Sabple Data" or "Create Sample Data from class" from the CreateSampleData button.
Bind the data to the ListPicker
And finally just rag and drop the Collection from the Data Panel onto the ListPicker. The CitiesDataSource is bound to the ListPicker which is now displaying the data as interactive items. This is done by Blend automatically generating a DataTemplate for the data. Run the Project to scroll the list of items.

You can see the generated XAML code:
<DataTemplate x:Key="CityTemplate">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
<toolkit:ListPicker VerticalAlignment="Top" ItemTemplate="{StaticResource CityTemplate}" ItemsSource="{Binding CityCollection}"/>
I this post I demonstrated how to bind a Windows Phone 7 ListPicker to XML file using ExpressionBlend just with a few mouse clicks.
Hope you enjoy this article. The full source code is available here.
You can also follow us on Twitter @winphonegeek
Comments
posted by: on 11/16/2010 7:16:36 PM
posted by: on 11/16/2010 10:29:16 PM
SelectedItem
posted by: benneh on 12/10/2010 3:46:41 PM
Hi, your example worked great, one question though.. how do you then retrieve the currently selectedItem value? I can't seem to figure it out?
RE:SelectedItem
posted by: winphonegeek on 12/13/2010 9:25:50 PM
If you take a look at the example you will see that the ItemSource is actually a List of strings:
this.list.ItemsSource = new List<string>{"ListItem1","ListItem2","ListIte3","ListItem4","ListItem5"};
So when you want to retrieve the selected item value you need to use this code:
string selectedItem = this.list.SelectedItem as string;
However if you want to retrieve the selected item container then the code is:
ListBoxItem selectedItemContainer = this.list.ItemContainerGenerator.ContainerFromItem(this.list.SelectedItem) as ListBoxItem;
Writing to the xml file
posted by: Chris on 12/7/2011 12:57:01 AM
Hi, thanks for the tutorial it works great, but how would i go about writing back to this file, I.e. I select Spain from the list and the data i input on the phone will be saved over Madrid?
Regards Chris
another tutprial?
posted by: justin land on 4/9/2012 2:46:49 PM
i started this tutorial its exacly what i wanted but right off teh bat i couldent falow it because i dont have the directorys listed now thtat wp7 sdk is 7.1 insted of 7.0
the directory...
- Start a new WP7 application project and add reference to: C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Nov10\Bin\Microsoft.Phone.Controls.Toolkit.dll
dose not exist!
is thare a more up to date tutorial?
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
