c# - Bind outside ItemsSource of DataTemplate, Windows phone -
in windows phone 8 have longlistselector , itemtemplate specified that. in code behind set itemssource longlistselector. in item template want bind value outside itemssource. how that?
<datatemplate x:key="template"> <textblock text="{binding name}"/> <textblock text="{binding country}"/> </datatemplate> ... <phone:longlistselector x:name="list" itemtemplate="{staticresource template}"> </phone:longlistselector>
c#
string country = "japan"; this.list.itemssource = items;
so how bind country outside itemssource? country accessor in "code behind" phoneapplicationpage.
it best make models inside template have bind item model.
anyway possible bind outside of itemsource:
xaml:
<phone:phoneapplicationpage.resources> <datatemplate x:key="itemtemplate"> <stackpanel> <textblock text="{binding name}"/> <!-- binds layoutroot's datacontext, can setted "code behind" --> <textblock text="{binding datacontext.outside, elementname=layoutroot}"/> </stackpanel> </datatemplate> </phone:phoneapplicationpage.resources> <!--layoutroot root grid page content placed--> <grid x:name="layoutroot"> <phone:longlistselector itemssource="{binding items}" isgroupingenabled="false" itemtemplate="{staticresource itemtemplate}"> </phone:longlistselector> </grid>
in cs have of course properties:
public observablecollection<model> items{get; set;} public string outside { get; set; }
also layoutroot's datacontext should setted somewhere in cs:
layoutroot.datacontext = this;
Comments
Post a Comment