c# - How do I set a DataTemplate for a WPF TreeView to display all Elements of an List? -


i'd visualize following data structure using treeviews in wpf:

class mydatacontext {     icollectionview outers {get;set;}     //... }  class outer {     string name {get;set;}     ienumberable<inner> actions {get;set;}  }   class inner {     string description {get;set;}     command onclick {get;set;} } 

this attempt far:

<!-- datacontext mydatacontext @  point --> <treeview itemssource="{binding path=outers}">     <treeview.resources>         <datatemplate datatype="{x:type myns:outer}">             <stackpanel orientation="horizontal">                 <textblock text="{binding path=name}"/>                  <treeview itemssource="{binding path=actions}" >                     <datatemplate datatype="{x:type myns:inner}">                         <button command={binding path=onclick}>                             <textblock text="{binding path=description}"/>                         </button>                     </datatemplate>                 </treeview>             </stackpanel>         </datatemplate>     </treeview.resources> </treeview> 

it seams there's wrong access since following invalidoperationexception:

operation not valid while itemssource in use. access , modify elements itemscontrol.itemssource instead. 

if drop inner treeview there's no exception (but no buttons of course).

i used page mateusz mentioned (hierarchicaldatatemplate) , after reading answer question: bind collection stackpanel found solution did wanted:

here players (level 3) on same row team (level 2):

<treeview itemssource="{binding league}">     <!-- conference template -->     <treeview.itemtemplate>         <hierarchicaldatatemplate itemssource="{binding teams}">             <textblock foreground="red" text="{binding name}" />             <!-- team template -->             <hierarchicaldatatemplate.itemtemplate>                 <datatemplate>                     <stackpanel orientation="horizontal">                         <textblock text="{binding name}"/>                         <itemscontrol itemssource="{binding players}">                             <itemscontrol.itemspanel>                                 <itemspaneltemplate>                                     <stackpanel orientation="horizontal">                                     </stackpanel>                                 </itemspaneltemplate>                             </itemscontrol.itemspanel>                             <itemscontrol.itemtemplate>                                 <datatemplate>                                     <button content="{binding }"/>                                 </datatemplate>                             </itemscontrol.itemtemplate>                         </itemscontrol>                     </stackpanel>                 </datatemplate>             </hierarchicaldatatemplate.itemtemplate>         </hierarchicaldatatemplate>     </treeview.itemtemplate> </treeview> 

the result


Comments