c# - how to load the datatemplate in a listbox as per the number of data in xml file -


i have task list in xml file,i want bind label,one after other on each label in each template,i unable load template per number of task in xml file.,the number of task should equal number of templates , label should contain name of task,i m stuck here code data template

 <window.resources>             <datatemplate x:key="tasktemplate1">                 <canvas height="50" width="850">                     <label   height="30" width="170" canvas.top="10" canvas.left="130" background="lightgray">                     </label>                     <textbox height="30" width="120" canvas.top="10" canvas.left="370" background="lightblue"></textbox>                     <label canvas.left="500" canvas.top="10">$</label>                     <button click="deletebuttonclick" canvas.top="12" height="10" width="30" canvas.left="600" ></button>                 </canvas>             </datatemplate>         </window.resources> 

for listbox code this

 <tabitem>         <canvas height="700" width="850">              <listbox itemtemplate="{staticresource tasktemplate1}" itemssource="{binding namelist}"  x:name="listbox" height="700" width="850">             </listbox>             <label canvas.top="-18" canvas.left="185">select task</label>             <label canvas.top="-18" canvas.left="377" rendertransformorigin="0.58,0.462">enter bill rates</label>             <button canvas.left="39" canvas.top="575" width="139">click add task</button>         </canvas>     </tabitem> 

currently binding namelist have manually created behind code depending on code 4 datatemplates created,but not able task xml file on label of template

 private void window_loaded(object sender, routedeventargs e)     {         xmldocument doc1 = new xmldocument();         doc1.load("http://store.tymesheet.com/templates/software-developer.xml");         xmlelement root = doc1.documentelement;         xmlnodelist nodes = root.selectnodes("tasks/task");         string temp=null;         foreach(xmlnode node in nodes)         {             temp = node["name"].innertext;         }          _namelist = new list<string>                     {                       "1",                       "2",                       "3",                       "4",                     };         datacontext = this;     }      private list<string> _namelist;     public list<string> namelist     {         { return _namelist; }     } 

the screenshot xml file enter image description here

the screen shot of of template here

enter image description here

if need name of task, can directly in xaml using xmldataprovider act itemssource listbox.

<canvas height="700" width="850">     <canvas.resources>         <xmldataprovider x:key="tasks" xpath="tasks"            source="http://store.tymesheet.com/templates/software-developer.xml"/>         <datatemplate x:key="tasktemplate1">             <canvas height="50" width="850" background="lightgray">                 <label content="{binding xpath=name}" height="30"                        width="170" canvas.top="10" canvas.left="150"                         background="lightgray"/>                 <textbox height="30" width="60" canvas.top="10"                          canvas.left="370" background="black"/>                 <label canvas.left="500" canvas.top="10">$</label>                 <button click="deletebuttonclick"                          canvas.top="12" height="10" width="30"                         canvas.left="600"/>              </canvas>         </datatemplate>     </canvas.resources>     <listbox itemtemplate="{staticresource tasktemplate1}"           itemssource="{binding path=childnodes, source={staticresource tasks}}"            x:name="listbox" height="700" width="850"/>     .... </canvas> 

set path childnodes , in label bind xpath name.


update

if want in code behind, have fill list , set itemssource list. (make sure add namespace system.linq use extension methods select, oftype etc.)

xmldocument doc = new xmldocument(); doc.load("http://store.tymesheet.com/templates/software-developer.xml"); var tasklist = doc.childnodes.oftype<xmlnode>()                .where(node => node.name == "tasks")                .selectmany(node => node.childnodes.oftype<xmlnode>())                .select(node => node["name"].innertext); listbox.itemssource = tasklist; 

and in xaml:

bind label content binding this:

<canvas height="700" width="850">     <canvas.resources>         <datatemplate x:key="tasktemplate1">             <canvas height="50" width="850" background="lightgray">                 <label content="{binding}"      <-- here                        height="30" width="170"                        canvas.top="10" canvas.left="150"                        background="lightgray"/>                 <textbox height="30" width="60" canvas.top="10"                          canvas.left="370"                           background="black"/>                 <label canvas.left="500" canvas.top="10">$</label>                 <button click="deletebuttonclick" canvas.top="12" height="10"                          width="30" canvas.left="600"/>             </canvas>         </datatemplate>     </canvas.resources>     <listbox itemtemplate="{staticresource tasktemplate1}"                 x:name="listbox" height="700" width="850"/>     .... </canvas> 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

asp.net - Passing parameter to telerik popup -

node.js - ldapjs - write after end error -