Wednesday, 24 July 2013

LongListSelector: item click (Windows Phone)

Use the SelectionChanged event of LongListSelector to handle the item click action. Windows Phone APIs are still young and doesn't have a straight forward and out of the box API to handle the click event yet. But if you use the SelectionChanged event wisely its a good alternative.

in XAML:
<phone:LongListSelector x:Name="LngLstSel" SelectionChanged="LngLstSel_SelectionChanged">

In CodeBehind (C#):
private void LngLstSel_SelectionChanged(object sender, SelectionChangedEventArgs e) {
  // If selected item is null, do nothing
  if (LngLstSel.SelectedItem != null)
  {
  // Place your logic here.

  // Reset selected item to null
  LngLstSel.SelectedItem = null;
  }
}

Just make sure to check that the LongListSelector's SelectedItem property is not null and after doing your actions in the event, set the LongListSelector's SelectedItem property to null. This will clear off the bug that can happen if you want to select the same item consequently, because the event raises when an item selection is changed.

No comments:

Post a Comment