Entity class:
Employee class:
public class Department:observablecollection<employee>
{
public string Depname {get; set;}
Public observablecollection<employee> Employees {get; set;}
}
Department Category:
public class Employee
{
public int number {get; set;}//Work No.
public string name {get; set;}//Name
public string Sex {get; set;}//Gender
public int Birthyear {get; set;}//Year of birth
}
Department Collection Class:
public class Departmentlist:observablecollection<department>
{
Public departmentlist ()
{
observablecollection<employee> employee1 = new observablecollection<employee>
{
New Employee{number=2012,name= "Netboy", sex= "Boy", birthyear=1992},
New Employee{number=2013,name= "Dandan", sex= "Girl", birthyear=2000},
New Employee{number=2014,name= "Xiaobai", sex= "Girl", birthyear=2012}
};
observablecollection<employee> employee2 = new observablecollection<employee>
{
New Employee{number=2020,name= "Kaizi", sex= "Girl", birthyear=2011},
New Employee{number=2021,name= "Yangzai", sex= "Gril", birthyear=2010}
};
This. ADD (new Department {depname = "technical Department", Employees = employee1});
This. ADD (new Department {depname = "mofcom", Employees = Employee2});
Observablecollection<department> deparment = new observablecollection<department>
//{
New Department{depname= "Tengfei", employees=employee1},
New Department{depname= "Google", employees=employee2}
//};
}
UI,XAML:
<window x:class= "Collectionviewsourcedemo.mainwindow"
xmlns= "http://schemas.microsoft.com/winfx/2006/ Xaml/presentation "
xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:local=" Clr-namespace: Collectionviewsourcedemo.models "
title=" MainWindow "height=" "width=";
<Window.Resources>
<local:departmentlist x:key= "Deplist"/>
<collectionviewsource x:key= "Departmentview"
Source= " {StaticResource resourcekey=deplist} "/>
<datatemplate x:key=" dtemployees ";
<stackpanel height=" "
horizontalalignment=" Center "
width=" 480 "
verticalalignment=" Top "
orientation=" Horizontal ">
<textblock height= "50"
Horizontalalignment= "left"
Width= "90"
text= "{Binding number}"/>
<textblock height= "50"
Width= "120"
text= "{Binding Name}"/>
<textblock height= "50"
Width= "120"
text= "{Binding birthyear}"/>
<textblock height= "50"
Width= "120"
text= "{Binding Sex}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<textblock width= "" "height=" fontsize= "text=" Please select Department: "/>
<listbox name= "LB1" height= "156" width= "
Displaymemberpath= "Depname"
Itemssource= "{Binding source={staticresource Departmentview}}"
Horizontalalignment= "Center"
Verticalalignment= "Top" fontsize= "/>"
<textblock height= "62"
Width= "111"
Horizontalalignment= "left"
Verticalalignment= "Top"
text= "{Binding path=depname,source={staticresource Departmentview}}"
Foreground= "Red" fontsize= "/>"
<textblock text= "Employee List" fontsize= "foreground=" Blue/>
<stackpanel orientation= "Horizontal" >
<textblock text= "Work No." margin= "5"/>
<textblock text= "name" margin= "70,5,5,5"/>
<textblock text= "date of birth" margin= "70,5,5,5"/>
<textblock text= "Gender" margin= "75,5,5,5"/>
</StackPanel>
<listbox name= "lb2" itemssource= "{Binding path=employees,source={staticresource Departmentview}}"
Itemtemplate= "{StaticResource dtemployees}"/>
</StackPanel>
</Grid>
</Window>
The application of WPF CollectionViewSource