c# - Binding from parents Viewmodel -
i have view has listbox
in it. have listbox
bound collection of listboxviewmodel property of mainviewmodel. have datatemplate
listbox
in im binding properties of listboxviewmodel. view contains listbox has datacontext set mainveiwmodel. how can bind properties of mainviewmodel in datatemplate
of listbox
has itemsource
bound collection of listboxviewmodels?
this combobox
in listbox
datatemplate
has itemsoucrce
bound collection of listboxviewmodels. notice im trying bind mainviewmodel properties listboxviewmodel properties in datatemplate
<listbox itemsource="{binding path=collectionoflistboxviewmodelsinmainviewmodel}" <datatemplate> ..... <combobox margin="6" width="300" iseditable="true" itemssource="{binding path=mainviewmodelproperty}" //binding not working selecteditem="{binding listboxviewmodelproperty}" //binding works text="{binding listboxviewmodelproperty, updatesourcetrigger=lostfocus}"/> //binding works ..... </datatemplate> </listbox>
simplest way:
<yourwindow x:name="mywindow"> <listbox itemsource="{binding path=collectionoflistboxviewmodelsinmainviewmodel}" <datatemplate> ..... <combobox margin="6" width="300" iseditable="true" itemssource="{binding elementname=mywindow, path=datacontext.mainviewmodelproperty}" selecteditem="{binding listboxviewmodelproperty}" //binding works text="{binding listboxviewmodelproperty, updatesourcetrigger=lostfocus}"/> //binding works ..... </datatemplate> </listbox> </yourwindow>
1) remember mywindow must have set datacontext mainviewmodel. can use ui element, doesn't have window. 2) second solution use relativesource instead of elementname.
Comments
Post a Comment