Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to bind Data to ListBox in Windows Phone

DZone's Guide to

How to bind Data to ListBox in Windows Phone

· Mobile Zone
Free Resource

Get gorgeous, multi-touch charts for your iOS application with just a few lines of code.

The below code demonstrates a really simple way of binding data to the Standard ListBox control in Windows Phone 7 using C# .

Assuming , you have the Class Movie as described below

public class Movie
{
        public string Actor { get; set; }
        public string Name { get; set; }
 
}

Create a List of Movie with the Data .

public class MovieList : List<Movie>
    {
        public MovieList()
        {
            Add(new Movie { Name = "Kaavalan", Actor = "Vijay", });
            Add(new Movie { Name = "Velayutham", Actor = "Vijay",  });
            Add(new Movie { Name = "7th Sense", Actor = "Surya",  });
            Add(new Movie { Name = "Billa 2", Actor = "Ajith Kumar",  });
            Add(new Movie { Name = "Nanban", Actor = "Vijay",  });
 
        }
    }

No create the ListBox in the XAML page and add the ItemTemplate to the ListBox along with a textbox to display the Name as described below

<ListBox Height="596" Name="listBox1" Width="380" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=Name}"></TextBlock>                     
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
</ListBox>

Now , assign the MovieList to the Listbox's ItemSource Property

public MainPage()
{
            InitializeComponent();
            FillListBox();
        }
 
        private void FillListBox()
        {
            listBox1.ItemsSource = new MovieList();
        }   
}

Run the Windows Phone Application by pressing F5 to launch the Windows Phone Emulator .

.Net developers: use Highcharts, the industry's leading interactive charting library, without writing a single line of JavaScript.

Topics:

Published at DZone with permission of Senthil Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}