How to Select Items From ListBox

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
  • Data Binding

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.   
  13. namespace DataList  
  14. {  
  15.   
  16.     public partial class MainPage : PhoneApplicationPage  
  17.     {  
  18.         // Constructor  
  19.         public MainPage()  
  20.         {  
  21.   
  22.             InitializeComponent();  
  23.             this.DataContext = GetData();  
  24.         }  
  25.         private List<BloodDoner> GetData()  
  26.         {  
  27.             List<BloodDoner> lstBloodDoner = new List<BloodDoner>  
  28.              
  29.                                 {  
  30.                                     new BloodDoner { Name ="Ram" , Id  ="Analyst" , PhoneNo = 9063737 },  
  31.                                     new BloodDoner { Name ="Shyam" ,Id  ="Tester" , PhoneNo = 237563766 },  
  32.                                     new BloodDoner { Name ="Mohan" ,  Id  ="Developer" , PhoneNo = 93792389 },  
  33.                                     new BloodDoner { Name ="Ramesh" ,  Id  ="Programmer" , PhoneNo = 3737676 },  
  34.                                     new BloodDoner { Name ="Michel" ,  Id  ="Professional" , PhoneNo = 35113676 },  
  35.                                  };  
  36.             return lstBloodDoner;  
  37.   
  38.         }  
  39.         private void lstBloodCorner_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  40.         {  
  41.   
  42.             BloodDoner data = (sender as ListBox).SelectedItem as BloodDoner;  
  43.             PhoneCallTask phoneCallTask = new PhoneCallTask();  
  44.             phoneCallTask.PhoneNumber = data.PhoneNo.ToString();  
  45.             phoneCallTask.DisplayName = data.Name;  
  46.             phoneCallTask.Show();  
  47.         }  
  48.     }  
  49.     public class BloodDoner  
  50.     {  
  51.         public string Name { getset; }  
  52.         public string Id { getset; }  
  53.         public int PhoneNo { getset; }  
  54.     }  
  55. }  
  56.   
  57.           
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.


Similar Articles