c# - Trouble with Binding Item in WP8 -
i developed take-note app, used itemsnote save list of notes, , itemmodifynote save temporary item when modify.
public observablecollection<noteviewmodel> itemsnote { { return _itemsnote; } set { _itemsnote = value; notifypropertychanged("itemsnote"); } } public noteviewmodel itemmodifynote { get; set; }
at mainpage.xaml ( display itemsnote binding within longlistselector), insert "edit" button next each note, when click it, set itemmodifynote's data selected item in itemsnote, navigate "modifynotepage.xaml"
private void bteditnote_click(object sender, routedeventargs e) { var button = (sender button).datacontext noteviewmodel; if (button != null) { int intindex = app.viewmodel.itemsnote.indexof(button); string modifyuri = "/pages/notemodifypage.xaml?id=" + intindex.tostring(); app.viewmodel.itemmodifynote = app.viewmodel.itemsnote.elementat(intindex); navigationservice.navigate(new uri(modifyuri, urikind.relativeorabsolute)); } }
at modifynotepage.xaml, modify data of itemmodifynote (which include title , content, both string) 2 textbox
<textbox grid.column="1" text="{binding itemmodifynote.notetitle, mode=twoway}" x:name="tbxmodifynotetitle" fontfamily="clear sans light" borderthickness="0.0" keydown="tbxmodifynotetitle_keydown"/> </grid> <textbox grid.row="1" margin="0,0,0,20" x:name="tbxmodifynotecontent" text="{binding itemmodifynote.notecontent, mode=twoway}" acceptsreturn="true" textwrapping="wrap" borderthickness="0.0" fontfamily="clear sans light" gotfocus="tbxmodifynotecontent_gotfocus" lostfocus="tbxmodifynotecontent_lostfocus"/>
finally use 2 buttons: cancel , save.
in save button set data of item in itemsnote data of itemmodifynote
private void btcancel_click(object sender, eventargs e) { navigationservice.navigate(new uri("/mainpage.xaml", urikind.relativeorabsolute)); } private void btsave_click(object sender, eventargs e) { app.viewmodel.itemsnote[key] = app.viewmodel.itemmodifynote; navigationservice.navigate(new uri("/mainpage.xaml", urikind.relativeorabsolute)); }
the problem is: when click cancel button, note still save modify text ???
that's because itemmodifynote
references noteviewmodel
instance itemsnote
. since both textbox
in edit page , longlistselector
in main page operating on same veiwmodel instance, when user modify itemmodifynote
property, longlistselector
display updated value without more code needed.
to avoid behavior, in button edit click event handler method, try create new instance of noteviewmodel
, copy it's properties value 1 in itemsnote
, instead of directly referencing existing instance.
Comments
Post a Comment