c# - ListView Delete Refresh does not work? -
i'm developing wpf mvvm application basic crud functionality. use listview contains , show data users. when add or update user listvew refreshing immediately, after delete command listview not refreshing. in case have close application , run again make refresh listview.
xaml
<listview name="lstusers" scrollviewer.horizontalscrollbarvisibility="disabled" scrollviewer.verticalscrollbarvisibility="disabled" issynchronizedwithcurrentitem="true" itemssource="{binding viewlist.view, updatesourcetrigger=propertychanged, isasync=true}" selecteditem="{binding currentselecteduser, mode=twoway}" height="150" margin="0,40,10,260" grid.columnspan="2" grid.rowspan="2">
view model
private observablecollection<user> _users; public userviewmodel() { _users = new observablecollection<user>(getallusers()); // paging control viewlist = new collectionviewsource(); viewlist.source = users; viewlist.filter += new filtereventhandler(viewfilter); currentpageindex = 0; itemcount = users.count; calculatetotalpages(); } #region deletecommand public icommand deleteusercmd { { return new relaycommand(k => deleteuser(currentselecteduser)); } } // delete user database. private void deleteuser (user user) { if(currentselecteduser != null) { using (var usermanager = new usermanager()) { messageboxresult msgresult = messagebox.show("are sure delete user?", "delete user?", system.windows.messageboxbutton.okcancel, messageboximage.asterisk); if (msgresult == system.windows.messageboxresult.yes) { return; } usermanager.delete(user); messagebox.show("user deleted!", "confirm!", messageboxbutton.ok, messageboximage.asterisk); // refresh listview after delete record. users.remove(user); } } else messagebox.show("you must select user!", "warning!", messageboxbutton.ok, messageboximage.exclamation); } #endregion
model
public collectionviewsource viewlist { get; set; } public observablecollection<user> users { { return _users; } set { _users = value; onpropertychanged("users"); } }
edit: translation error? notice korisnici bosnian "the users"
what viewlist.view you're binding itemssource to?
anyway, you're presumably binding items property "users", hence why calling onpropertychanged("users") when want update list.
however, you're removing user "korisnici":
// refresh listview-a after delete record. korisnici.remove(user); onpropertychanged("users");
not "users" collection. unless korisnici.remove method have created? korisnici? think want:
// refresh listview-a after delete record. users.remove(user);
note, don't need onpropertychanged("users") - managed observablecollection, implements inotifycollectionchanged notify of changes collection.
Comments
Post a Comment