1

Heey Stackoverflowers

I am currently busy with a listbox with a List<> binded to it. I fill this List<> by a api call to our web server. Now when i reach the bottom of the list i display an "Load More" button when i press the button i start up another call to our api but then for 20 new items. Now is my Question how can i Add those 20 new items to my List<> without deleting the older 20.

This is how i fill the List<> at the moment

eventList = (from item in events.Descendants("item")
             select new Events
             {
             EventId = Convert.ToInt16(item.Element("eventid").Value),
             EventName = item.Element("eventname").Value,
             EventType = Convert.ToInt16(item.Element("type").Value)
             }).ToList();

Dispatcher.BeginInvoke(new Action(
() =>
     lbVanAlles.ItemsSource = eventList
));

But if i do this with my 20 new items they overwrite my old ones. So anybody got any clue? Probably something with eventList.Add but then i get errors that i cant assign it to a "method group"

MrME
  • 337
  • 2
  • 14

1 Answers1

4

Instead of overwriting eventList every time with the result of the Linq query, use the List.AddRange method to append items to the existing list.

var temp = from item in events.Descendants("item")
             select new Events
             {
             EventId = Convert.ToInt16(item.Element("eventid").Value),
             EventName = item.Element("eventname").Value,
             EventType = Convert.ToInt16(item.Element("type").Value)
             };
eventList.AddRange( temp );

Also, instead of reassigning lbVanAlles.ItemsSource each time you can switch over to using an ObservableCollection instead of a List. This will notify the ListBox when items are added and it will update automatically.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • Heey Prætorian i used your Answer and it worked :D. But the items dont show op when i Dispatch it and set the ItemsSource only if i do ItemsSource = eventList.OrderBy(etc.) I tryed making it an ObservableCollection but then i get some Convert Generic.List to ObjectModel.ObservableCollection errors and i cant seem to fix all the errors with this http://stackoverflow.com/questions/3559821/how-to-convert-ienumerable-to-observablecollection Got any thoughts on this problem? Already thanks for your help so far ^^ – MrME Apr 13 '12 at 12:27
  • @MrME The items don't show up now because you're trying to set the same `eventList` object (which has had items added to it) instead of a new object that was created by the query. You can call `lbVanAlles.UpdateLayout()` to force redrawing but the better solution is to use an `ObservableCollection` instead of a `List`. However, since `ObservableCollection` doesn't have any methods that will append an `IEnumerable` range to an existing collection, you'll have to add the results of the Linq query to the collection using a `foreach` loop. – Praetorian Apr 13 '12 at 14:37
  • Eventually we made them into `ObservableCollection` and fixed all our problems. Ty for helping us ^^ – MrME Apr 15 '12 at 15:02