标签:
实体类:
员工类:
public class Department : ObservableCollection<Employee>
{
public string DepName { get; set; }
public ObservableCollection<Employee> Employees { get; set; }
}
部门类:
public class Employee
{
public int Number { get; set; } //工号
public string Name { get; set; } //姓名
public string Sex { get; set; } //性别
public int BirthYear { get; set; } //出生年份
}
部门集合类:
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 = "技术部", Employees = employee1 });
this.Add(new Department { DepName = "商务部", 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="500" Width="1000">
<Window.Resources>
<local:DepartmentList x:Key="deplist"/>
<CollectionViewSource x:Key="departmentView"
Source="{StaticResource ResourceKey=deplist}"/>
<DataTemplate x:Key="dtEmployees">
<StackPanel Height="50"
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="200" Height="50" FontSize="36" Text="请选择部门:"/>
<ListBox Name="lb1" Height="100" Width="156"
DisplayMemberPath="DepName"
ItemsSource="{Binding Source={StaticResource departmentView}}"
HorizontalAlignment="Center"
VerticalAlignment="Top" FontSize="32" />
<TextBlock Height="62"
Width="111"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding Path=DepName,Source={StaticResource departmentView}}"
Foreground="Red" FontSize="32" />
<TextBlock Text="员工列表" FontSize="20" Foreground="Blue"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="工号" Margin="5" />
<TextBlock Text="名字" Margin="70,5,5,5"/>
<TextBlock Text="出生日期" Margin="70,5,5,5" />
<TextBlock Text="性别" Margin="75,5,5,5"/>
</StackPanel>
<ListBox Name="lb2" ItemsSource="{Binding Path=Employees,Source={StaticResource departmentView}}"
ItemTemplate="{StaticResource dtEmployees}" />
</StackPanel>
</Grid>
</Window>
标签:
原文地址:http://www.cnblogs.com/527289276qq/p/4315741.html