Working with WP7 ListPicker SelectedItem
published on: 5/4/2011 | Views: N/A | Tags: WP7Toolkit Binding ListPicker
by WindowsPhoneGeek
In this post I am going to talk about different techniques of accessing(manipulating) the SelectedItem of the Silverlight for WP7 Toolkit ListPicker control.
After we published "Working with WP7 ListBox SelectedItem" article we received lots of requests for a similar post related to the ListPicker SelectedItem issues. So in this article I will answer some of the questions with short examples.
NOTE: Generally ListPicker is a kind of ItemControl together with ListBox and other similar controls. This means that the basic structure is the same as well as the SelectedItem behavior.
![]()
When populating a ListPicker with items you have two potions:
Option1 -You can use ListPickerItems
Option2 - Data Binding
When using Option1 the SelectedItem is actually a ListPickerItems so it is easy to perform the desired manipulations. However when using Optio2 (which is actually 90% of the cases) the SelectedItem is the type of the business object (data class) which you use when performing data binding.
To begin with using ListPicker first add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly. You will also need to add the "toolkit" prefix declaration. Make sure that your page declaration includes the "toolkit" namespace:
<phone:PhoneApplicationPage ... xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
Lets say that we have a sample data bound ListPicker where SampleData is our business object (data class) :
public class SampleData
{
public string Day { get; set; }
public string Temperature { get; set; }
public int Number { get; set; }
}
<toolkit:ListPicker x:Name="listPicker" Header="Weekly Temperatures" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="YellowGreen" Width="50" Height="50" Margin="5">
<TextBlock Text="{Binding Number}" FontSize="30" HorizontalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding Day}" Width="150" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Day}" Width="150"/>
<TextBlock Text="{Binding Temperature}" Width="50"/>
<Button Click="Button_Click" Content="Button" Margin="0,-10,0,0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
Question 1: How to access the Selected ListPickerItem when having databound ListPicker ?
Answer: In order to get the selected ListPickerItem all you need to do is the following:
private void btnGetSelectedItem_Click(object sender, RoutedEventArgs e)
{
ListPickerItem selectedItem = this.listPicker.ItemContainerGenerator.ContainerFromItem(this.listPicker.SelectedItem) as ListPickerItem;
}
Question 2: How to access the SelectedItem from a ListPicker event handler?(when having databound ListPicker )
Answer: Lets say that we have subscribed to the ListPicker SelectionChanged event. At first we will get a reference to the current SelectedItem data object and after that we will get a reference to the SelectedItem container which is of type ListPickerItem.:
private void listPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Get the data object that represents the current selected item
SampleData data = (sender as ListPicker).SelectedItem as SampleData;
//Get the selected ListPickerItem container instance
ListPickerItem selectedItem = this.listPicker.ItemContainerGenerator.ContainerFromItem(data) as ListPickerItem;
}
Question 3: How to access the SelectedItem from a control placed inside ListPicker ItemTemplate/FullModeItemTemplate?
Answer: In this case we will add a Button inside the ListPicker FullModeItemTemplate and will subscribe to its Click event. As you can see below you can get a reference to the SelectedItem data object through the DataContext and after that you can use this data object to get a reference to the Selected ListPickerItem :
private void Button_Click(object sender, RoutedEventArgs e)
{
SampleData data = (sender as Button).DataContext as SampleData;
ListPickerItem pressedItem = this.listPicker.ItemContainerGenerator.ContainerFromItem(data) as ListPickerItem;
if (pressedItem != null)
{
//do something
}
}
Question 4: How to set the SelectedItem when having databound ListPicker?
Answer: The easiest way to set the SelectedItem of a ListPicker is to use its SelectedIndex property:
private void btnSetSelectedIndex_Click(object sender, RoutedEventArgs e)
{
this.listPicker.SelectedIndex = 1;
}
NOTE: SelectedIndex = 1 means the second item!
Question 5: How to access the Selected ListPickerItem and its data if I know the SelectedIndex? (when having databound ListPicker)
Answer: In this case at first we will get a reference to the selected ListPickerItem usingItemContainerGenerator.ContainerFromIndex and after that we will get the real data through the DataContext:
private void btnSelectedIndex_Click(object sender, RoutedEventArgs e)
{
ListPickerItem selectedItem = this.listPicker.ItemContainerGenerator.ContainerFromIndex(2) as ListPickerItem;
SampleData data = selectedItem.DataContext as SampleData;
}
NOTE: In some cases if you need to reference the selected item before the ListPicker is initialized you will have to use a Dispatcher. For example:
Dispatcher.BeginInvoke(() =>
{ ListPickerItem selectedItem = this.listPicker.ItemContainerGenerator.ContainerFromIndex(2) as ListPickerItem; });
That was all about the ListPicker SelectedItem in Windows Phone 7. You can find the full source code here:
I hope that this post was helpful.
You may also may find interesting some of our previous articles related to WP7 ListPicker:
- How to customize the ListPicker selected item
- Customizing ListPicker for WP7 - Part1
- Customizing ListPicker for WP7 - Part2
- ListPicker FullScreen mode Background problem: workaround
- Data Binding the WP7 ListPicker to XML data using ExpressionBlend 4
You can also follow us on Twitter @winphonegeek
Comments
Can I Databind the SelectedItem?
posted by: Kornelis on 5/5/2011 9:54:23 AM
Hi,
I'm using the MVVM Toolkit within my Project and wanted to DataBind the SelectedItem to a Property within my ViewModel so it gets automaticly filled each time the selections changeds and other way around the ListPicker automaticly show the correct item from its source when i set the Property in the ViewModel to one of the ItemsSource. But every time i try this i get an Exception.
Do you have a good Tip to use the ListPicker / SelectedItem according to the MVVM Toolkit?
Thanks and Kind Regards
Kornelis
Can I Databind the SelectedItem
posted by: winphonegeek on 5/5/2011 9:38:02 PM
Thank you for your suggestion. We have added your suggestion in our "To Do" list, so we will publish article related to ListPicker / SelectedItem according to the MVVM Toolkit as spoon as possible.
Software Developer
posted by: Brian on 9/16/2011 4:33:51 PM
Really good info for this brand new phone developer. Only, I don't see an example of what I need... programmatically selecting an item by name. For example, I have a class called BenefitFactors.cs that contains two properties, "Description" (string) and "Factor" (decimal).
I thought I could select an item like this, but it's not working:
lpBenefitFactor.SelectedItem = lpBenefitFactor.FindName("2.0%");
(The "2.0%" is actually my description.)
Can you tell me what I am doing wrong?
Thanks for any assistance.
listpicker binding (Microsoft bug)
posted by: Brian on 9/16/2011 11:31:56 PM
Okay, I think I am on the right road finally. Basically, the listpicker is not being updated because of a Microsoft bug. The option is to override the page OnNavigatedTo event. The code will look something like this:
protected override void OnNavigatedTo (System.Windows.Navigation.NavigationEventArgs e) {
base.OnNavigatedTo(e);
Binding pickerBinding = new Binding("Description") {
Source = App.Current.Resources["Settings"],
Mode = BindingMode.TwoWay
};
lpBenefitFactor.SetBinding(ListPicker.SelectedItemProperty, pickerBinding); // blows up here
}
My code is still blowing up on the very last line with the error: "SelectedItem must always be set to a valid value."
I am going to keep beating on it and will report back here for others when/if I find the solution.
ListPicker, the dumbest control on the planet
posted by: dj on 9/21/2011 12:38:21 AM
Could they make is any more difficult to use I mean come on. A combo box in winforms takes seconds to use. After an hour i still haven't got the damned thing to work.
I'm reading xaml books galore and scouring the web and have no clue how the damned thins is supposed to work if you add anything more complex than plan text to the thing. I am sure I am missing something but bloody hell what the f**k are they playing at.
Oh yes another thing
posted by: dj on 9/21/2011 1:04:05 AM
I would stand a change if the examples given actually worked.
Why can't I just set in in code and be done with it
Seem like things are made difficult just for the sake of it.
sorted
posted by: dj on 9/21/2011 1:29:11 AM
As usual I just coded my way around the problem. Why we can't just do everything in code I don't know.
XAML, as good as it is its just another headache to make simple things difficult IMHO
And I've just been singing praises about MS
I will study more XAML as I guess I will have no choice but where I can I will avoid it like the plague. I can do without this tripe
Pretty Dumb Control
posted by: waleed on 1/17/2012 8:14:08 PM
This so-called control is full of bullshit, seems like the guys at ms didn't test it. It's full of bugs and problems, I can't imaging someone could produce a low quality control like this one!!
Binding in XAML
posted by: Rajeev Nair on 3/16/2012 11:39:16 PM
If we are binding SelectedItem in XAML, we wouldn't need the code behind code rt?
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
