Using a ListBox, Dynamically adding contents to a listbox in Windows Phone

By | June 29, 2013

This post shows how to add string objects to a ListBox Control in Windows Phone.

Make sure to choose a C# template for the project.

This is the layout .

ListBox Windows Phone

This is the complete Layout
Just change the class name in the root of the layout to yours.

<phone:PhoneApplicationPage 
    x:Class="ListBoxExample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY LISTBOX APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="ListBox Application" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox Height="383" HorizontalAlignment="Left" Margin="3,7,0,0" Name="listBox1" VerticalAlignment="Top" Width="448" />
            <Button Content="Add to ListBox" Height="72" HorizontalAlignment="Left" Margin="86,499,0,0" Name="button1" VerticalAlignment="Top" Width="250" Click="button1_Click" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="12,414,0,0" Name="textBlock1" Text="Type something to add to the ListBox" VerticalAlignment="Top" Width="443" />
            <TextBox Height="72" HorizontalAlignment="Left" Margin="3,436,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="460" />
        </Grid>
    </Grid>
 </phone:PhoneApplicationPage>

Now we will look at the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ListBoxExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            listBox1.Items.Add("ListBox Demo From Coderzheaven"); 
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            listBox1.Items.Add(textBox1.Text.Trim());
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *