views:

164

answers:

1

When I set the ItemsSource of a ListBox to the contents of a table, like this:

this.listBox.ItemsSource = db.Table;

The items are not updated automatically in the ListBox. How can I manage to update the ListBox automatically when items are added, removed or changed? And can I also receive an event when the collection has changed?

A: 

Take a look at ObservableCollection. I'm using it to update/add/delete a listview. When I change the ObservableCollection, the listview gets notified.

photo_tom
I know about ObservableCollection, but how can I notify the ObservableCollection about inserts and other operations? Do I have to do all this manually? Isn't there some kind of library that automatically raises the ObservableCollection's change notification event when InsertOnSubmit or maybe SubmitChanges is called?
eWolf
For inserts I use ObservableCollection.Add(newRec). Similar methods exist for updates and deletes, but I've not gotten that far in my project.
photo_tom
So if I add an item to the ObservableCollection, it is automatically inserted into the database?
eWolf
No. Adding to ObservableCollection will automatically update display only.I'm using linq2sql, so what I do is create the item being added, call my linq2sql repository class to add, then add to observable. Works pretty good. Then when I close the popup, I have the repository class do a SubmitChanges. This way, popup user can do all adds/deletes/edit they want. But if they just want to cancel everything, by exiting popup without doing SubmitChanges, the DB is not changed.
photo_tom