This article explains how to select items in a list box in Windows Phone 8.

Generally a List Box is a kind of ItemControl together with ListPicker and other similar controls. This means that the basic structure and the SelectedItem behavior are the same.

There are the following two options in a List Box for selecting items.

ListBoxItems: When using this option the SelectedItem is actually a ListBoxItem so it is easy to apply this option.

Data Binding: It is the process that establishes a connection between the application and the business logic.

Now here, I am using ListBoxItems.

Code of XAML

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
  2. <ListBox x:Name="lstBloodCorner" ItemsSource="{Binding}" SelectionChanged="lstBloodCorner_SelectionChanged" >
  3. <ListBox.ItemTemplate>
  4. <DataTemplate>
  5. <StackPanel Orientation="Vertical">
  6. <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}" />
  7. <StackPanel Orientation="Horizontal" >
  8. <TextBlock Text="{Binding Id}" Style="{StaticResource PhoneTextNormalStyle}" />
  9. <TextBlock x:Name="TextBlock" Text="{Binding PhoneNo}" Style="{StaticResource PhoneTextAccentStyle }"/>
  10. </StackPanel>
  11. </StackPanel>
  12. </DataTemplate>
  13. </ListBox.ItemTemplate>
  14. </ListBox>
  15. </Grid>
Code of xaml.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Navigation;
  8. using Microsoft.Phone.Controls;
  9. using Microsoft.Phone.Shell;
  10. using DataList.Resources;
  11. using Microsoft.Phone.Tasks;
  12. namespace DataList
  13. {
  14. public partial class MainPage : PhoneApplicationPage
  15. {
  16. // Constructor
  17. public MainPage()
  18. {
  19. InitializeComponent();
  20. this.DataContext = GetData();
  21. }
  22. private List<BloodDoner> GetData()
  23. {
  24. List<BloodDoner> lstBloodDoner = new List<BloodDoner>
  25. {
  26. new BloodDoner { Name ="Ram" , Id ="Analyst" , PhoneNo = 9063737 },
  27. new BloodDoner { Name ="Shyam" ,Id ="Tester" , PhoneNo = 237563766 },
  28. new BloodDoner { Name ="Mohan" , Id ="Developer" , PhoneNo = 93792389 },
  29. new BloodDoner { Name ="Ramesh" , Id ="Programmer" , PhoneNo = 3737676 },
  30. new BloodDoner { Name ="Michel" , Id ="Professional" , PhoneNo = 35113676 },
  31. };
  32. return lstBloodDoner;
  33. }
  34. private void lstBloodCorner_SelectionChanged(object sender, SelectionChangedEventArgs e)
  35. {
  36. BloodDoner data = (sender as ListBox).SelectedItem as BloodDoner;
  37. PhoneCallTask phoneCallTask = new PhoneCallTask();
  38. phoneCallTask.PhoneNumber = data.PhoneNo.ToString();
  39. phoneCallTask.DisplayName = data.Name;
  40. phoneCallTask.Show();
  41. }
  42. }
  43. public class BloodDoner
  44. {
  45. public string Name { get; set; }
  46. public string Id { get; set; }
  47. public int PhoneNo { get; set; }
  48. }
  49. }
Then, I run the application and select an item. When I select an item from the list then that made a call automatically because I applied the code of the Phone Call in the selected items.

image1

Here, I select a phone number from the list, then automatically make a call.

image2

Summary

This article described how to select an item from a list box in Windows Phone 8.