[ACCEPTED]-Error: Items collection must be empty before using ItemsSource-windows-phone-7

Accepted answer
Score: 15

You want to create the list only once and 3 assign the data source only once! Therefore, create 2 the list before the loop and assign the data source 1 after the loop

// Clear the listbox.
// If you never add items with listBox1.Items.Add(item); you can drop this statement.

// Create the list once.
List<Taskonlistbox> dataSource = new List<Taskonlistbox>(); 

// Loop through the tasks and add items to the list.
for (int i = 0; i < tasks.Count(); i++) { 
    dataSource.Add(new Taskonlistbox {Text = "Blalalalala"} ); 

// Assign the list to the `ItemsSouce` of the listbox once.
this.listBox1.ItemsSource = dataSource;

More Related questions