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
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <ListBox x:Name="lstBloodCorner" ItemsSource="{Binding}" SelectionChanged="lstBloodCorner_SelectionChanged" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Vertical">
- <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}" />
- <StackPanel Orientation="Horizontal" >
- <TextBlock Text="{Binding Id}" Style="{StaticResource PhoneTextNormalStyle}" />
- <TextBlock x:Name="TextBlock" Text="{Binding PhoneNo}" Style="{StaticResource PhoneTextAccentStyle }"/>
- </StackPanel>
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- </Grid>
Code of xaml.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Navigation;
- using Microsoft.Phone.Controls;
- using Microsoft.Phone.Shell;
- using DataList.Resources;
- using Microsoft.Phone.Tasks;
-
- namespace DataList
- {
-
- public partial class MainPage : PhoneApplicationPage
- {
-
- public MainPage()
- {
-
- InitializeComponent();
- this.DataContext = GetData();
- }
- private List<BloodDoner> GetData()
- {
- List<BloodDoner> lstBloodDoner = new List<BloodDoner>
-
- {
- new BloodDoner { Name ="Ram" , Id ="Analyst" , PhoneNo = 9063737 },
- new BloodDoner { Name ="Shyam" ,Id ="Tester" , PhoneNo = 237563766 },
- new BloodDoner { Name ="Mohan" , Id ="Developer" , PhoneNo = 93792389 },
- new BloodDoner { Name ="Ramesh" , Id ="Programmer" , PhoneNo = 3737676 },
- new BloodDoner { Name ="Michel" , Id ="Professional" , PhoneNo = 35113676 },
- };
- return lstBloodDoner;
-
- }
- private void lstBloodCorner_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
-
- BloodDoner data = (sender as ListBox).SelectedItem as BloodDoner;
- PhoneCallTask phoneCallTask = new PhoneCallTask();
- phoneCallTask.PhoneNumber = data.PhoneNo.ToString();
- phoneCallTask.DisplayName = data.Name;
- phoneCallTask.Show();
- }
- }
- public class BloodDoner
- {
- public string Name { get; set; }
- public string Id { get; set; }
- public int PhoneNo { get; set; }
- }
- }
-
-
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.
Here, I select a phone number from the list, then automatically make a call.
Summary
This article described how to select an item from a list box in Windows Phone 8.