Dynamic Update of ListBox in Windows Store App


In this article we will use a List Box control and two buttons to implement dynamicly update a data list from the front end. In this application we can add content to the list by entering the content in a TextBox and with the help of an Add button. There is no limit to how many items that can be added. In the reverse process we also delete the items from the list. To delete the item from list box, first of all we will select the item that we want to delete and click on the Delete button.

In the following we are putting the entire code of the XAML file and code behind file to create this mini application. 

Step 1 : First, you will create a new Metro Style Application. Let us see the description with images of how you will create it.

  • Open Visual Studio 2011
  • File -> New -> Project
  • Choose Template -> Visual C# -> Metro Style Application
  • Rename the application


Step 2 : In the Solution Explorer there are two files that we will primarily work with; the BlankPage.xaml and BlankPage.xaml.cs files.


Step 3 : The BlankPage.xaml file is as in the following code:

Code :












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


            <ColumnDefinition Width=".133*"/>

            <ColumnDefinition Width=".333*"/>



            <RowDefinition Height=".033*"/>

            <RowDefinition Height=".080*"/>

            <RowDefinition Height=".333*"/>


        <TextBlock Grid.Column="0" Grid.Row="0" Text="Dynamic Updation" FontSize="30" HorizontalAlignment="Right"></TextBlock>

        <TextBlock Grid.Column="1" Grid.Row="0" Text="  in List Box" FontSize="30" HorizontalAlignment="Left"></TextBlock>

       <TextBox x:Name="txt1" Background="Yellow" Grid.Column="0" Grid.Row="1" Height="50" Width="300"

        HorizontalAlignment="Center" VerticalAlignment="Top">


        <Button Content="ADD" Background="Red" Grid.Column="1" Grid.Row="1" Height="50" Width="200"

         Margin="0,0,0,0" VerticalAlignment="Top" Click="Button_Click_1">


        <Button Content="DELETE" Background="Green" Grid.Column="1" Grid.Row="1" Height="50" Width="200"

         Margin="0,50,0,0" VerticalAlignment="Top" Click="Button_Click_2">


        <ListBox x:Name="listb1" Grid.Column="0" Grid.Row="2" Height="300" Width="300"

         Background="LightCyan" VerticalAlignment="Top" HorizontalAlignment="Center" FontWeight="Bold" Foreground="Black" FontSize="25">




Step 4 : 
The BlankPage.xaml.cs file is as in the following code:

Code :

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;


namespace Application8



   public sealed partial class BlankPage : Page


        public BlankPage()




       protected override void OnNavigatedTo(NavigationEventArgs e)




        private void Button_Click_1(object sender, RoutedEventArgs e)





        private void Button_Click_2(object sender, RoutedEventArgs e)







5 : After running this code the output looks like this:


To add data to the list box enter the value into the text box and click on the add button.


As simply you can add many items as you want.


In the reverse process delete the items from list box, select the item and click on the delete button.


You will find that the selected item has been removed from the list.


Similar Articles