c# - Why is my view not updating on a view model change? -
parent view model command method in possalesviewmodel
know might have broken concept of mvvm code below i'm new , improvement appreciated.
private void addproduct(productdto productdto) { var ctx = (possalesdetailsviewmodel)((possalesdetailsview)tabitems[_selectedtabindex].content).datacontext; ctx.productlines.add(new productline() { product = productdto.description, quantity = 1, producttradechannelid = productdto.producttradechannelid, amount = productdto.amount }); }
child view model possalesdetailsviewmodel
below collection reached view not being updated
public observablecollection<productline> productlines { => _productlines; set { set(ref _productlines, value); } }
here child view binding collection
<datagrid x:name="producttypesdatagrid" itemssource="{binding productlines, mode=twoway}" autogeneratecolumns="false" canusersortcolumns="true" canuseraddrows="false"> <datagrid.columns> <materialdesign:materialdatagridtextcolumn isreadonly="true" binding="{binding product, mode=twoway}" header="product" width="auto" /> <materialdesign:materialdatagridtextcolumn isreadonly="true" binding="{binding quantity, mode=twoway}" header="quantity" width="auto" /> <materialdesign:materialdatagridtextcolumn isreadonly="true" binding="{binding amount, mode=twoway}" header="price" width="auto" /> <datagridtemplatecolumn width="auto"> <datagridtemplatecolumn.celltemplate> <datatemplate> <button content="delete" command="{binding datacontext.deletecommand, relativesource={relativesource ancestortype={x:type datagrid}}}" commandparameter="{binding}" /> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid.columns> </datagrid>
note
the child view model instance being created in constructor new instance each time (not mvvmlight way)
public possalesdetailsview() { initializecomponent(); this.datacontext = new possalesdetailsviewmodel(); }
after debugging issue figured out issue was.
<tabcontrol grid.row="0" grid.rowspan="2" grid.column="1" itemssource="{binding tabitems}" selectedindex="{binding selectedtabindex}"> <tabcontrol.itemtemplate> <!-- header template--> <datatemplate> <textblock text="{binding header}" /> </datatemplate> </tabcontrol.itemtemplate> <tabcontrol.contenttemplate> <!-- body of tabitem template--> <datatemplate> <local:possalesdetailsview/>//**problem here** </datatemplate> </tabcontrol.contenttemplate> </tabcontrol>
the problem user control instance created on parent view load. when requested adding new user control inside tab item new instance created. , data not appearing. moved user control section.
<datatemplate> <grid> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> </grid.rowdefinitions> <textblock text="{binding header}" /> <local:possalesdetailsview grid.row="1"/> // **here** </grid> </datatemplate> </tabcontrol.itemtemplate>
Comments
Post a Comment