Windows Phone 7 - Binding Data to ListBox through Code

Windows Phone 7 - Binding Data to ListBox through Code

  • Comments 10

In Windows Phone 7 we need to display data programmatically. Below one demonstrates the simply way of binding data through code.

Suppose you have Emp class as below.

public class Emp
    public int Id { get; set; }
    public string Name { get; set; }

Let’s create List<Emp>

List<Emp> myData = new List<Emp>()
    new Emp(){Id = 1, Name = "Wriju"},
    new Emp(){Id = 2, Name = "Writam"},
    new Emp(){Id = 3, Name = "Saswati"},
    new Emp(){Id = 4, Name = "Wrishika"},
    new Emp(){Id = 5, Name = "Baba"},
    new Emp(){Id = 6, Name = "Ma"}

After that format the ListBox to display it properly.

    <ListBox Name="listBoxEmployee">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Id}"></TextBlock>
                    <TextBlock Text=" - "></TextBlock>
                    <TextBlock Text="{Binding Path=Name}"></TextBlock>

Now, you need to simply code to bind the data

lstData.ItemsSource = myData;

This is a very simple example but useful in many scenario and applied to most of the data-bound application.


Leave a Comment
  • Please add 2 and 5 and type the answer here:
  • Post
  • The item "lstData" appears only when you assign the ItemsSource to it. I think you might have to actually type this out in Visual Studio for yourself to get it to work.

    Also, how about dynamic updating once the values change. What do we do to update the listBoxEmployee?

  • great!

    i'm looking for this!

    easy, clean works!


  • Thank you so much!

  • Daniel, Daniel, I'm also trying to search solution for dynamic uploading - I try to use INotifyPropertyChanged. Maybe that will help you.

  • ok. this may be simple for understanding but all fine when page load. try to refresh the list with data binding later point without page load and you have music. :)

  • Thanks for this, but its not working.

  • Hi,

    I was hoping you could help me, What if i want to bind to more than 1000 items, how will i handle that?

  • For large list you should consider

  • Great article.

  • Thanks nice example

Page 1 of 1 (10 items)