Binding WPF Treeview using linq query -


i have 4 table in sql server 2012

departmentstbl ============== depid ------- depname 1     ------- human resources 2     ------- financial management  specialtbls: ============ spclid ------- spclname 1     ------- manager 2     ------- secretary 3     ------- data entry  employeestbl: ============ empid ------- empname 1     ------- jack 2     ------- mark 3     ------- sara  jobdescriptiontbls: =================== jdid ------- empid ------- depid ------- spclid  1    ------- 1     ------- 1     -------  1 2    ------- 2     ------- 1     -------  2 3    ------- 2     ------- 1     -------  3 

note (some times departments has no employees & must appear in treeview )

and want show data in treeview according departments names such that

depname ------- first node specialname --- second node empfullname --- third node 

i use linq query data , xaml this:

xaml:

<treeview.itemtemplate>   <hierarchicaldatatemplate itemssource="{binding path=departmentstbls}">     <textblock text="{binding path=depname}"                foreground="#fff59e13" />     <hierarchicaldatatemplate.itemtemplate>       <datatemplate>         <textblock text="{binding path=specialname}"                    foreground="white" />       </datatemplate>       <hierarchicaldatatemplate.itemtemplate>         <datatemplate>           <textblock text="{binding path=empfullname}"                      foreground="white" />         </datatemplate>       </hierarchicaldatatemplate.itemtemplate>     </hierarchicaldatatemplate.itemtemplate> </treeview.itemtemplate> 

linq:

var employees = (from spcl in menof.specailtbls                          join deps in menof.departmentstbls                          on spcl.soecialid equals deps.depid                          //from deps in menof.departmentstbls                          join ejd in menof.empjobdescriptiontbls                          on deps.depid equals ejd.depid                          join emps in menof.employeestbles                          on ejd.empid equals emps.empid                          select new { spcl.specialname,deps.depname,emps.empfullname }).tolist();         tvempsname.itemssource = employees; 

but data doesn't appear correctly first node appear. question wrong here. thanks.

here short working example of hierarchicaldatatemplate.

xaml

<window x:class="wpfapplication1.treeviewexample"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:local="clr-namespace:wpfapplication1"         title="treeviewexample" height="600" width="600"         loaded="window_loaded">     <grid>         <treeview itemssource="{binding artists}">             <treeview.resources>                 <hierarchicaldatatemplate   datatype="{x:type local:artist}" itemssource="{binding albums}" >                     <textblock text="{binding artistname}"/>                 </hierarchicaldatatemplate>                 <hierarchicaldatatemplate  datatype="{x:type local:album}" itemssource="{binding tracks}" >                     <textblock text="{binding albumname}"/>                 </hierarchicaldatatemplate>                 <datatemplate datatype="{x:type local:track}">                     <textblock text="{binding trackname}"/>                 </datatemplate>             </treeview.resources>         </treeview>     </grid> </window> 

code behind

public partial class treeviewexample : window, inotifypropertychanged {     public treeviewexample() {         initializecomponent();         this.datacontext = this;     }      public list<artist> artists { get; set; }      public event propertychangedeventhandler propertychanged;      //populate sample data     private void window_loaded(object sender, routedeventargs e) {          //first artist node data         list<track> tracks11 = new list<track> { new track("track111"), new track("track112") };         list<track> tracks12 = new list<track> { new track("track121"), new track("track122") };         list<album> albums1 = new list<album> { new album("album11", tracks11), new album("album12", tracks12) };          //second artist node data         list<track> tracks21 = new list<track> { new track("track211"), new track("track212") };         list<track> tracks22 = new list<track> { new track("track221"), new track("track222") };         list<album> albums2 = new list<album> { new album("album21", tracks21), new album("album22", tracks22) };          artists = new list<artist> { new artist("artist1", albums1), new artist("artist2", albums2) };         propertychanged(this, new propertychangedeventargs("artists"));     } }  public class artist {     public artist(string artistname, list<album> albums) {         this.artistname = artistname;         this.albums = albums;     }     public string artistname { get; set; }     public list<album> albums { get; set; } }  public class album {     public album(string albumname, list<track> tracks) {         this.albumname = albumname;         this.tracks = tracks;     }     public string albumname { get; set; }     public list<track> tracks { get; set; } }  public class track {     public track(string trackname) {         this.trackname = trackname;     }     public string trackname { get; set; } } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -