c# - Binding my changing data context in WPF -
i've seen slight variations on request - i'm having hard time getting work situtaion. have application user selection of combobox drive event. grabs data database , want bind data ui. thought had binding set correclty, can't happen. can see code calculation firing correctly, don't see feedback regarding binding. i'm pretty new wpf , still trying wrap head around (i come mvc web world) pointers appreciated. thanks.
mainwindow code behind:
public mainwindow() { initializecomponent(); datacontext = new mainwindowviewmodel(); } private void cboallclients_selectionchanged(object sender, selectionchangedeventargs e) { mainwindowviewmodel vm = datacontext mainwindowviewmodel; listclient selectedclient = e.addeditems[0] listclient; credit.calc c = new credit.calc(); c.calccredit(selectedclient.clientname); vm.ffcredits = c.fundfamilycredits; vm.fundcredits = c.fundcredits; datacontext = vm; }
main window xaml (abbreviated binding issue)
<listbox x:name="lbfundfamily" width="auto" height="auto" background="transparent" borderthickness="0"> <listbox.datacontext> <binding source="ffcredits"></binding> </listbox.datacontext> <listbox.itemtemplate> <datatemplate> <grid> <grid.rowdefinitions> <rowdefinition height="20"></rowdefinition> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto"></columndefinition> </grid.columndefinitions> <textblock text="{binding path=fundfamilyname}" grid.row="0" grid.column="0" background="white" fontsize="14" foreground="black"> </textblock> <!--<rectangle width="{binding path=brokercreditlist.utilization}" height="20" grid.row="0" grid.column="1"></rectangle>--> <!--<textblock text="{binding path=brokercreditlist.}"></textblock>--> </grid> </datatemplate> </listbox.itemtemplate> </listbox>
mainwindowviewmodel:
class mainwindowviewmodel : baseviewmodel { public list<listclient> allclients { get; set; } public observablecollection<fundfamilysum> ffcredits { get; set; } public observablecollection<fundsum> fundcredits { get; set; } public mainwindowviewmodel() { using (agencymodelentities db = new agencymodelentities()) { allclients = db.listclients.tolist(); ffcredits = new observablecollection<fundfamilysum>(); fundcredits = new observablecollection<fundsum>(); } } }
initially when view loaded, listbox
bound vm.ffcredits
new observablecollection<fundfamilysum>();
but view not know ffcredits has changed unless propertychanged notification raised still bound new observablecollection<fundfamilysum>();
.
i think expecting changes picked setting datacontext = vm
again. not work vm
datacontext
.
Comments
Post a Comment