Binding List Box and List Of Strings in Windows Phone 8


In this article we will learn how to bind a list of strings with a List Box in Windows Phone 8. It's a great way of adding and removing the items from the List Box at run time. In this method we just add an empty List Box to our application and will populate it programmatically using data binding. So let's start.

Binding The List and List Box

In this article I'll focus only on the list of strings only. We want to bind the list of strings with the List Box. Binding means whenever you change the list, the List Box content will also change.

The following simple steps will help you in binding your List Box and list:

  1. Add a List Box to your application page.
  2. Provide it a name, such as itemList.
  3. Set the item source in XAML equal to binding.
  4. Create a list in code behind.
  5. Set the item source of the List Box equal to the Step 4 list.

The following code implements the preceding steps.











    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"



    <!--LayoutRoot is the root grid where all page content is placed-->

    <Grid x:Name="LayoutRoot" Background="Transparent">


            <RowDefinition Height="Auto"/>

            <RowDefinition Height="*"/>



        <!--TitlePanel contains the name of the application and page title-->

        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="Demo" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>

            <TextBlock Text="Binding-1" Margin="9,-7,0,0" FontSize="40"/>



        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <Button Name="pushBtn" Content="Add" Click="addClick" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="225"/>

            <Button Name="popBtn" Content="Remove" Click="popClick" HorizontalAlignment="Left" Margin="225,10,0,0" VerticalAlignment="Top" Width="221"/>

            <ListBox Name="itemList" ItemsSource="{Binding}" Margin="0,87,0,10"></ListBox>





C# Code Behind

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 Demo.Resources;

using System.Threading;

using Nokia.Music.Types;


namespace Demo


    public partial class MainPage : PhoneApplicationPage


        // Constructor

        public MainPage()




        private static int i = 0;

        public static List<string> list = new List<string>();

        private void popClick(object sender, RoutedEventArgs e)


            if (i>0)



                itemList.ItemsSource = null;

                itemList.ItemsSource = list;






        private void addClick(object sender, RoutedEventArgs e)


                list.Add("item " + ++i);

                itemList.ItemsSource = null;

                itemList.ItemsSource = list;





In the code above I'm resetting the item source each time to reflect the changes. It's not a preferred way of doing this but it's good for beginners. The best way to reflect the changes is to use INotify property Change or adding individual items using the add method of list items.



That's all for this article. I hope you have found it useful. In case of any doubt feel free to comment below. In my next article we will cover how to bind a dictionary with the List Box.

Similar Articles