WPF DataGrid Toolkit - Handling State

Jul 23, 2009 at 12:35 AM
Edited Jul 23, 2009 at 12:41 AM

First, I apologize if this has been addressed before. I scourged quite a bit and didn't find an answer.

The quickest question is this: Is there a way to force the DataGrid not to call EndEdit() or CancelEdit()?

My reasoning is because,

1. I have two view models, parent and child, both implement my base class EditableViewModel (IEO, IDEI, INPC). The ParentViewModel contains some properties and an ObservableCollection<ChildViewModel> collection.

2. A modal window is built, bound to the ParentViewModel. The parent's properties are provided in text boxes, save for the OC of children; it is bound to a DataGrid below. On the window's display the parent view model's BeginEdit() is called.

3. BeginEdit() makes a generic dictionary backup of all the view model properties (thank you to Mark Smith), and moves along.

4. The window has Ok and Cancel buttons. Ok is simple: it persists (EndEdit), marks all records as clean, and closes the window. The Cancel button, checks all records (parent and child) for IsDirty=true and double checks if the user would like to persist. If they say yes, the same process as Ok happens... if they say no, CancelEdit() is called and thus a method to restore the view model state from the dictionary.

Works fine for the parent records (text boxes bound to value type properties)... So herein lies the issue... the datagrid calls Begin/Cancel/EndEdit in its own way... creating/restoring/nullifying the child view model's saved state in its own transactions... this is good, except that the saved state of the parent view model cannot maintain the integrity of its children as they are saved as an ObservableCollection<T> (reference type).

Symptom Example: a user pops open a window... the grid record's value is 5 and saved away in parent's _savedState. They edit the grid and change it to 10. Grid raises EndEdit() and child's _savedState is nullified. At that point the parent's _savedState shows the referenced record, reflecting 10 as well rather than 5. :(

If I can prevent the DataGrid from calling EndEdit() and CancelEdit() (thus preserving their state for the life of the window) I can extend my Ok and Cancel buttons to loop through the OC and do it manually, just as i am for the parent.

The bottom line is my goal is to preserve the state of the children view models in the DataGrid for the life of the modal window, just like the parent view model. I suppose another plan of attack would be to enrich the Backup/Restore functionality within my EditableViewModel base class, but I cannot think of a way to do this since it should be ignorant to the types of properties any given view model implementing it contains.

If anyone has other ideas aside from my main question, I'm all ears. I'm a nooblet.

Thanks a ton,

Trey