Here is my requirement (I am simplifying things for the sake of this question). I have a list of people identified by first and last name. The list is stored in an XML file. I need to create a WPF application consisting of two windows: the main windows display a list. Nothing unusual, just a list with first and last name. Three buttons below: Add, Change, Delete. When you click the Delete button, the selected line is permanently deleted from the file. When you click the "Add" or "Edit" button, a second window (dialog) appears in which you can respectively enter a new person by entering his name and surname, or change and save the existing entry.
How do I create this application in MVVM? Here are the specific questions I have:
1) From an MVVM point of view, I cannot directly bind to an XML data source, or can I? So I have to create a Person object that implements INotifyPropertyChange and then save a bunch of these objects in an ObservableCollection. Accordingly, I need to write code that reads an XML file and converts it into a collection of objects, and vice versa, when I need to save the changes back to the file. I'm right?
2) Where can I put the implementation of the Persist method, which actually saves to an XML file? There are two places where this method will be called: click the "Delete" button on the main window or click the "OK" button on the "Add / Edit" window.
3) Please note that the same window handles "Add and Edit", how do I implement this window properly with MVVM? How to display the title of this window "Add" or "Edit" without resorting to ugly, if the code is expressed? To what I associate with the two text fields that I have (name and surname). Also note that in the "Add / Edit" window there are "OK" and "Cancel" buttons, changes are not accepted until the "OK" button is pressed, and if you click "Cancel", the changes should be discarded. One of the possible solutions that I see is to clone the Person object, and not directly contact the same object as the linked list, so that the list does not show changes until the OK button is pressed, but itโs hairy , since I need to write code to copy properties back and forth, is there a better way to do this in MVVM?
thanks in advance
Henry
source share