windows phone 7 - How do i refresh the datacontext on page -
i using mvvm in wp8 app. have landing page(allproducts.xaml) displays list of products. in constructor have this.datacontext = productsviewmodel;
in allproducts.xaml, have
listboxbind property(productlist) ofproductsviewmodel.an app bar add button takes user
addproduct.xamlpage. here, user adds new product gets saved in database. after save method called, havenavigationservice.goback();takes user previous page (allproducts.xaml)
however, allproducts.xaml shows newly added product. obvious & think because of navigationservice.goback(); restores state of previous page , not rebinds it.
how rebind/refresh page newly added product displayed in list?
here xaml code in allproducts.xaml bound productlist property of productsviewmodel
<phone:longlistselector itemssource="{binding getproductlist, mode=twoway}" name="lls" itemtemplate="{staticresource mydatatemplatehere}" toolkit:tilteffect.istiltenabled="true" selectionchanged="lls_selectionchanged"/> here productsviewmodel
public class productsviewmodel: inotifypropertychanged { private observablecollection<productlist> _productlist; public observablecollection<productlist> getproductlist { { var prodlist = p in unitofwork.productrepository.getall() join c in unitofwork.customerrepository.getall() on p.custid equals c.custid select new productlist { productid = p.id, productname = p.productname, customerid = c.custid}; _productlist= new observablecollection<productlist>(prodlist .tolist()); return _productlist; } set { _productlist= value; onpropertychanged("getproductlist"); } } public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } here productlist class.
public class productlist { public int productid { get; set; } public string productname { get; set; } public int customerid { get; set; } }
you gotta let binding know needs query getter.. after adding new item
raisepropertychanged(() => productsviewmodel); or make productsviewmodel observablecollection
Comments
Post a Comment