mvvm - Set TreeView HierarchicalDataTemplate with 2 levels in WPF -


i have view model represented in datacontext of window.

public class schoolviewmodel:viewmodelbase {       public observablecollection<schoolclassgroup> classescollection { get; set; }             ....              }    public class schooleclassgroup:viewmodelbase {      public string classname {get;set;}      public string teachername {get;set;}      public observablecollection<students> studentcollection{ get; set; }  }  public class student {      public string name {get;set;}      public int age {get;set;}      public datetime birthdate {get;set;}      ... }  

i want represent treeview show me schools,class , students.

how can it?

thanks!

you need create heirarchicaldatatemplates each level of object tree has parent-child relationship, plus simple datatemplate leaf node.

your object tree should school -> class -> student

class school has

list<class> 

class class has

list<student> 

then it's simple

<window.resources>          <hierarchicaldatatemplate itemssource="{binding classes}" datatype="{x:type school}">             <textblock text="{binding name}" />         </hierarchicaldatatemplate>          <hierarchicaldatatemplate itemssource="{binding students}" datatype="{x:type class}">             <textblock text="{binding name}" />         </hierarchicaldatatemplate>          <datatemplate datatype="{x:type student}">             <textblock text="{binding name}" />         </datatemplate >      </window.resources>      <grid>         <treeview itemssource="{binding schools}" >     </grid> 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -