views:

54

answers:

2

Hi All,

I have implemented the Drap n Drop functionality using the two listboxes (lstSource, lstDest) where user can drap & drop item between two lists. I have two ObservalableCollections (sourceCollection & destCollection) which are binded to the relevant lists. In the initial load method, DomainContext gets all the tasks from db and set it to source Collection. I then use the LINQ (Where t=> t.UserID == thisUserID) to get the subset of tasks from the source collection and set to destCollection. Now the User can add/remove tasks from the lstDest (destCollection).

My question is How can I save those changes back to the db when user presses the Save button.

I have checked the DomainContext.HasChanges which returns false so cannot use the context.SubmitChanges()

Any idea how to save those changes in the db with latest list of tasks for the user?

Any response would be appreciated.

A: 

When you move the items between collections you also need to set some of their properties to signify changes. ObservableCollection exposes the NotifyCollectionChanged event which you can use for that purpose. So the answer would be to set the User property of a Task to the current user when the Task enters the ListBox that signifies the current user's tasks, and remove it when it enters the collection of tasks.

Edit: Another technique I used, if the collection usually contains a moderate amount of items (what the limit is I don't know, you must test with appropriate test data), was to have all the items in the same collection (usually ObservableCollection to be able to add/remove items dynamically without having to worry about updating the user interface), and create two views (ICollectionView interface, usually created with CollectionViewSource in Silverlight I think) each having a filtering predicate. The view uses the predicate to determine if an item belongs to the view or not; so one view can have the predicate condition 'user must be equal to the current user' and the other one 'user must be null'.

Simply setting the user property of an entity in your collection (if the entity implements INotifyPropertyChanged) will automagically move the entity in the appropriate view. Granted, this will require changes to the drag and drop part of the problem: when dropping an item into a list, you need to set its user property to either the current user or null depending on the drop target, and then everything else is taken care of by the views.

The changes still need to be saved to the database - simply call SubmitChanges (at least that's what it's called in RIA services, I think... working with too many technologies at the moment) when the user clicks the 'ok' or 'save' button or whatever. The modified entities will be saved, and they will be detected as modified because their user property has been modified.

Alex Paven
would that means it will also update/notify the backend database as well?
Jhelumi786
No; INotifyCollectionChanges simply defines an event that is raised when the collection is changed, with arguments containing details about the changes. It depends on who listens to that event. By default, WPF listens to that event when the collection is bound to an ItemsControl so that adding and removing items dynamically is reflected properly in the user interface without anything else. You can listen to the same event.
Alex Paven
Something else you could do is hold the entities in the same collection and create two filtered views on top of that collection. Actually you know what, I'll update my answer with that suggestion.
Alex Paven
A: 

Thanks for detailed response Alex. Actually I need to keep the lists sorted as well in the order the user wants(by moving items up/down) so I used the seperate ObservableCollections. Whenever user adds/removes , change position of the items in the list I loop thru them, reset their index (priority), creates a new temp list and assign to my dest ObersvableCollection.

but I'll try what u suggested and let U know how it goes. My data is'nt too big.

Thanks once again for the detailed response.

Jhelumi786

related questions