c# - Preventing a Passed Model from Updating when the User Closes a Window in MVVM WPF -
this question has answer here:
- how selectively update model in wpf 1 answer
i have window
datagrid
companies
in it. user clicks 'edit', new window
opens , selectedcompany
passed modifywindow
. works expected, , if user clicks save
on modifywindow
selectedcompany
updated on sql , changed reflected in datagrid
on main companies
window.
however, if user makes changes company
in modifywindow
, not click save instead closes window
using cross (to me saying want throw away edit) company
still updated on datagrid
. here code:
from companiesviewmodel
:
private void showeditcompanywindow() { var modifycompanyviewmodel = new modifycompanyviewmodel(selectedcompany); modifycompanyviewmodel.returnmodifiedcompany += loadmodifiedcompany; messenger.default.send(modifycompanyviewmodel); } private void loadmodifiedcompany(company modifiedcompany) { var existingcompany = datagridcompanies.firstordefault(c => c.id == modifiedcompany.id); var = datagridcompanies.indexof(existingcompany); datagridcompanies[i] = modifiedcompany; var existingsearchcompany = allsearchcompanies.firstordefault(s => s.id == modifiedcompany.id); var x = allsearchcompanies.indexof(existingsearchcompany); if (existingsearchcompany != null) { existingsearchcompany.name = modifiedcompany.name; existingsearchcompany.town = modifiedcompany.town; allsearchcompanies[x] = existingsearchcompany; } selectedcompany = modifiedcompany; searchcompanyview = collectionviewsource.getdefaultview(allsearchcompanies) collectionview; application.current.mainwindow.activate(); }
from modifycompanyviewmodel
:
private async void processcompany(window window) { var waitview = new waitview { owner = application.current.windows.oftype<window>().firstordefault(x => x.isactive) }; waitview.show(); if (await editcompany()) { returnmodifiedcompany(selectedcompany); closewindow(window); } waitview.close(); }
the datagrid
on companiesview
:
<datagrid grid.row="1" x:name="companiesdatagrid" margin="5" itemssource="{binding datagridcompanies}" selecteditem="{binding selectedcompany}"> <datagrid.columns> <datagridtextcolumn header="name" width="*" binding="{binding name}"/> <datagridtextcolumn header="town" width="*" binding="{binding town}"/> <datagridtextcolumn header="telephone" width="*" binding="{binding telephone}"/> </datagrid.columns> </datagrid>
i'm new mvvm
, i'm thinking don't have updating of company
in datagridcompanies
mvvm
handle me. bigger issue though how selectedcompany
still changes in datagrid
though have closed modify window , not want commit. have tried binding closing event on modify window , storing unmodified company , return that, company
in datagrid
still changed regardless.
you example implement ieditableobject interface in company
class , call canceledit()
method when window closed.
the beginedit()
method should store "old" property values, e.g.:
string _oldname; public void beginedit() => _oldname = _name; public void canceledit() => name = _oldname; public void endedit() => _oldname = null;
Comments
Post a Comment