Improvements in the LongListSelector Selection with Nov `11 release of WP Toolkit
published on: 11/22/2011 | Views: N/A | Tags: WP7Toolkit
by WindowsPhoneGeek
In this article I am going to talk about some improvements that have been made to the LongListSelector control in the latest official release of Windows Phone Toolkit Nov `11.
Here is a common question that our readers have been asking about the LongListSelector:
"How to get Selected Item and Group info in the LongListSelector SelectionChaged event? "
The problem was that this was not possible until the Nov`11 release of the toolkit, because the arguments of the SelectionChanged event were practically useless since the type of the objects representing the selected items was private.
In the November update of the Toolkit this issue is already fixed and here is the official release note:
"ItemTuple is now refactored to be the public type LongListSelectorItem to provide users better access to the values in selection changed handlers"
Before you begin: For more information about how to use the LongListSelector control take a look at our FREE eBook: "Silverlight for Windows Phone Toolkit In Depth".
In this article we will use the example from our previous post: "WP7 LongListSelector in depth | Part2: Data binding scenarios". In short here is the data source should look like and how we should subscribe to the SelectionChanged event:
public MainPage()
{
InitializeComponent();
List<City> source = new List<City>();
// ...
source.Add(new City() { Name = "Roma", Country = "IT", Language = "Italian" });
source.Add(new City() { Name = "Paris", Country = "FR", Language = "French" });
var cityByCountry = from city in source
group city by city.Country into c
orderby c.Key
select new Group<City>(c.Key, c);
this.longListSelector.ItemsSource = cityByCountry;
this.longListSelector.SelectionChanged += new SelectionChangedEventHandler(longListSelector_SelectionChanged);
}
Next we will add the following code inside the LongListSelectro SelectionChanged handler:
void longListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
IList addedItems = e.AddedItems;
if (addedItems == null || addedItems.Count < 1)
{
return;
}
// get first selected item
LongListSelectorItem selectedItem = addedItems[0] as LongListSelectorItem;
if (selectedItem == null)
{
return;
}
StringBuilder message = new StringBuilder();
message.AppendLine("Selected item:");
message.AppendLine(string.Format("Type: {0}", selectedItem.ItemType));
message.AppendLine(string.Format("Group: {0}", selectedItem.Group));
message.AppendLine(string.Format("Item: {0}", selectedItem.Item));
MessageBox.Show(message.ToString());
}
Items of type LongListSelectorItem are used to represent selected items. This type has the following important properties:
- ItemType: the type of the selected item.
- Group: holds a reference to the group of the selected item (in our case this will be an object of type Group<City>)
- Item: holds a reference to the selected item (in our case this will be an object of type City)
Here is the final result:
That was all about how to get Selected Item and Group info in the LongListSelector SelectionChaged event. You can find the full source code here:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Thank you
posted by: ebattulga on 12/1/2011 5:27:39 PM
Your work save my time. Thank you
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
