i've done research on topic, , while i've come across possibilities, nothing has worked me.
details:
i'm working on wpf app using mvvm design pattern. in viewmodel, have list
of notes
, class few properties (among them, note
). i've created property, selectednote
on vm
hold selected note.
in view
, i've bound listview
control list qcnotes
. i've bound textbox
selectednote
property. when make changes textbox
, correctly reflected in appropriate row of listview
.
problem:
i've include revertchanges
command. relatively simple command undoes changes i've made note. correctly updates textbox
, , updates underlying list correctly, changes not update listview
itself. (is necessary use observablecollection
in circumstance? i've been asked try , resolve problem without doing so).
attempted fixes
i tried call notifypropertychanged("selectednote")
, notifypropertychanged("qcnotes")
directly within call revertchanges
, hasn't fixed problem.
any ideas?
xaml
<window.datacontext> <vm:mainprojectviewmodel /> </window.datacontext> <grid> <stackpanel> <listview itemssource="{binding qcnotes, updatesourcetrigger=propertychanged, mode=twoway}" x:name="list" selecteditem="{binding selectednote}"> <listview.view> <gridview> <gridviewcolumn header="note" displaymemberbinding="{binding note}" /> </gridview> </listview.view> </listview> <textbox height="30" horizontalalignment="stretch" text="{binding selectednote.note, updatesourcetrigger=propertychanged, mode=twoway}" /> <stackpanel orientation="horizontal" horizontalalignment="center"> <button content="allow edits" command="{binding changestatetoalloweditscommand}" /> <button content="save changes" command="{binding editnotecommand}" /> <button content="revert changes" command="{binding revertchangestonotecommand}" /> </stackpanel> </stackpanel> </grid>
viewmodel code
public class mainviewmodel : baseviewmodel { private qcnote selectednote; private string oldnoteforupdating; private vmstate currentstate; private string noteinput; private ilist<qcnote> qcnotes; public ilist<qcnote> qcnotes { { return qcnotes; } set { qcnotes = value; notifypropertchanged(); } } public qcnote selectednote { { return selectednote; } set { selectednote = value; oldnoteforupdating = selectednote.note; notifypropertchanged(); } } public vmstate currentstate { { return currentstate; } set { currentstate = value; notifypropertchanged(); } } public icommand revertchangestonotecommand { { return new actioncommand(o => revertchangestonote()); } } private void revertchangestonote() { qcnotes.first(q => q.id == selectednote.id).note = oldnoteforupdating; selectednote.note = oldnoteforupdating; notifypropertchanged("selectednote"); notifypropertchanged("qcnotes"); currentstate = vmstate.view; }
i'll post answer own question, don't want deter other offering suggestions.
i implemented inotifypropertychanged
interface on models.qcnote
class, , resolved issue. initially, interface implemented exclusively on viewmodel
. in case, notifypropertychanged
called when qcnote
object changed, not when properties of object changed.
Comments
Post a Comment