I am having a problem with DomainContext.RejectChanges() and reflecting the rollback in the UI. Here is my scenario.
- I have a Model (Entity) generated for use with RIA services (I’ll call it Foo)
- I have a ViewModel that wraps Foo and extends it (I’ll call it FooViewModel)
- I have a View that is using Binding to display and update data using the FooViewModel
- I have an “outer” ViewModel that holds an ObservableCollection of FooViewModels
- The “outer” View has a list box bound to the ObservableCollection
So essentially there is a listbox of FooViewModels on one screen…when you select an item a childwindow is displayed to edit that particular FooViewModel. The FooViewModel is serving both the listbox and the childwindow.
Editing works just fine. A change in the childwindow reflects in the listbox immediately because I am calling RaisePropertyChanged() when the viewmodel properties are updated.
However, If I perform a DomainContext.RejectChanges()…the underlying entity gets rolled back (all changes reverted as expected)…however the FooViewModel isn’t aware that this change has occurred and thus the UI isn’t updated. If I reselect the item in the listbox on the first screen, the childwindow is displayed with the rolled back changes (which is what I want). The listbox still isn’t updated though.
When I reject changes, if I kludge a RaiseProperyChanged() for the field that I changed…the UI listbox does update.
How do I get the UI to update when the underlying entity is rejected?? And how do I do it without tracking what properties of the viewmodel were rolledback? There has to be an easy way to accomplish this that I am just missing.
Something you could try is use the PropertyChanged event on the underlying entity
Footo trigger a RaisePropertyChanged pass on theFooViewModelproperties.so making some assumptions (so this code make sense):
You have a private variables in your
FooViewModelprivate Foo _foo;private DomainContext _context;You have a method on your
FooViewModelthat is callingRejectChanges()on your domain context.Like so:
FooViewModelLike so:
Ok, now we have that established, lets have a look at exactly what happens when you call
RejectChanges()on a domain context.When you call
RejectChanges()this bubbles down through theDomainContextto itsEntityContainer, then to eachEntitySetin that container and then to eachEntityin the set.Once there (and in the
EntitySet), it reapplies the original values if there was any, removes the entity if it was added, or adds it if it was deleted. If there was changes to the values, then it applies them back to the properties.So theoretically, all the RaisePropertyChanged(), that are generated in the entity properties, should be triggered.
NOTE: I haven’t actually tested this. If this isn’t the case, then none of this works 😛
So we can hook into PropertyChanged event of the
Fooentity, and raise thePropertyChangedevent on ourFooViewModel.so our
RejectChanges()method might look like this:So we hook up an event handler to our
Fooentity, which calls theFooViewModel.RaisePropertyChangedmethod with the property name that is changing on theFooentity.Then we reject changes (which triggers the property changes),
then we unhook the event handler.
Pretty long winded, but I hope this helps 🙂