ListPicker Item in Windows Phone 8 App

Introduction

Recently, I was working on the Windows Phone 8 Application and in which I had to use the ListPicker from the Windows Phone Toolkit.

So, in this blog we will use the Windows Phone Toolkit ListPicker. Proceed with the following procedure:

Adding Windows Phone Toolkit

Install the Windows Phone Toolkit using the NuGet Gallery.

Windows Phone Toolkit Nuget Reference

As you can see that this toolkit is already installed in my app.

Design XAML Page

Now Design the page as the following code:

  1. <phone:PhoneApplicationPage  
  2.     x:Class="BloodCornerApp.Page2"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.             xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"  
  10.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  11.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  12.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  13.     Foreground="{StaticResource PhoneForegroundBrush}"  
  14.     SupportedOrientations="Portrait" Orientation="Portrait"  
  15.     shell:SystemTray.IsVisible="True">  
  16.    
  17.     <!--LayoutRoot is the root grid where all page content is placed-->  
  18.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  19.         <Grid.Resources>  
  20.             <DataTemplate x:Name="PickerItemTemplate">  
  21.                 <StackPanel Orientation="Horizontal">  
  22.                     <Border Background="LightGreen" Width="34" Height="34">  
  23.                         <TextBlock Text="{Binding BloodGroupItems}" FontSize="16" HorizontalAlignment="Center"   
  24.                                 VerticalAlignment="Center"/>  
  25.                     </Border>  
  26.                     <TextBlock Text="{Binding BloodGroupItems}" Margin="12 0 0 0"/>  
  27.                 </StackPanel>  
  28.             </DataTemplate>  
  29.             <DataTemplate x:Name="PickerFullModeItemTemplate">  
  30.                 <StackPanel Orientation="Horizontal" Margin="16 21 0 20">  
  31.                     <TextBlock Text="{Binding BloodGroupItems}" Margin="16 0 0 0" 
                                FontSize="43" 
    FontFamily="{StaticResource PhoneFontFamilyLight}"/>  
  32.                 </StackPanel>  
  33.             </DataTemplate>  
  34.         </Grid.Resources>  
  35.         <Grid.RowDefinitions>  
  36.             <RowDefinition Height="Auto"/>  
  37.             <RowDefinition Height="*"/>  
  38.         </Grid.RowDefinitions>  
  39.    
  40.         <!--TitlePanel contains the name of the application and page title-->  
  41.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  42.             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>  
  43.             <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  44.         </StackPanel>  
  45.    
  46.         <!--ContentPanel - place additional content here-->  
  47.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  48.             <Grid.RowDefinitions>  
  49.                 <RowDefinition Height="150"/>  
  50.                 <RowDefinition Height="*"/>  
  51.             </Grid.RowDefinitions>  
  52.             <toolkit:ListPicker Grid.Row="0" x:Name="listPicker" ItemTemplate="{StaticResource PickerItemTemplate}"  
  53.                      FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"    
  54.                          FullModeHeader="Cities" SelectedIndex="2" 
                             CacheMode="BitmapCache" Header="Cities" />  
  55.             <toolkit:ListPicker Header="Default" Grid.Row="1" x:Name="defaultPicker"/>   
  56.         </Grid>  
  57.     </Grid>   
  58. </phone:PhoneApplicationPage>  

Code

Add the following code:

  1. public partial class Page2 : PhoneApplicationPage  
  2. {  
  3.     public Page2()  
  4.     {  
  5.         InitializeComponent();  
  6.              listPicker.SetValue  
  7. (Microsoft.Phone.Controls.ListPicker.ItemCountThresholdProperty, 3);  
  8.         List<BloodGroupDetail> source = new List<BloodGroupDetail>();  
  9.         source.Add(new BloodGroupDetail() { BloodGroupItems = "A+" });  
  10.         source.Add(new BloodGroupDetail() { BloodGroupItems = "B+" });  
  11.         source.Add(new BloodGroupDetail() { BloodGroupItems = "O+" });  
  12.         source.Add(new BloodGroupDetail() { BloodGroupItems = "AB+" });  
  13.         this.listPicker.ItemsSource = source;  
  14.     }  
  15.    
  16.     public class BloodGroupDetail  
  17.     {  
  18.         public string BloodGroupItems { getset; }  
  19.     }    
  20. }  

Page

As you can see that the following the page of ListPicker

 ListPicker in Windows Phone

Now click on the option, which will redirect you to the ListPicker items

 Use of ListPicker in Windows Phone

Ebook Download
View all
Learn
View all