0" FontSize="32" /TextBlockHorizontalAlignment="Right"Text="

时间:2021-07-22 09:10:14

在windows phone中绑定调集数据的时候,有时候需要分层数据,凡是需要以主从试图形式显示。凡是的要领是将第二个ListBox(主视图)的数据源绑定到第一个ListBox

(从视图)的SelectedItem,或者通过第一个ListBox的SelectionChanged事件来设置绑定。但是借助CollectionViewSource类可以更便利的实现;

CollectionView是一个调集视图类,撑持数据的排序、分组、过滤。对数据的映像进行摆列组合;

CollectionViewSource是CollectionView的一个XAML代办代理,可以在XAML中使用;

案例说明:用主从试图关系显示两个的员工列表;效果图如下:

0" FontSize="32" /TextBlockHorizontalAlignment="Right"Text="

前期事情,,创建三个类来初始数据源;

(1)Employee.cs

public class Employee { public int Number { get; set; } //工号 public string Name { get; set; } //姓名 public string Sex { get; set; } //性别 public int BirthYear { get; set; } //出生年份 }

(2)Department.cs

public class Department:ObservableCollection<Employee> { public string DepName { get; set; } public ObservableCollection<Employee> Employees { get; set; } }

(3)DepartmentList.cs

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} //}; } }

注意:使用ObservableCollection<T>的时候需要引用定名空间——using System.Collections.ObjectModel;

通过在新建页面的phone:PhoneApplicationPage符号中添加一个定名空间映射。代码如下:

xmlns:local="clr-namespace:数据绑定"//我的项目为“数据绑定”

添加资源字典:

<phone:PhoneApplicationPage.Resources> <local:DepartmentList x:Key="deplist"/> <CollectionViewSource x:Key="departmentView" Source="{StaticResource deplist}"/> <DataTemplate x:Key="dtEmployees"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top" Orientation="Horizontal"> <TextBlock HorizontalAlignment="Left" Text="{Binding Number}"/> <TextBlock Text="{Binding Name}"/> <TextBlock Text="{Binding BirthYear}"/> <TextBlock Text="{Binding Sex}"/> </StackPanel> </DataTemplate> </phone:PhoneApplicationPage.Resources>

在构造页面中添加如下代码:

<TextBlock FontSize="36" Text="请选择部门:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,30,0,0"/> <ListBox DisplayMemberPath="DepName" ItemsSource="{Binding Source={StaticResource departmentView}}" Margin="40,86,260,0" HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="32" /> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding Path=DepName,Source={StaticResource departmentView}}" Foreground="Red" Margin="12,210,0,0" FontSize="32" /> <TextBlock HorizontalAlignment="Right" Text="员工列表" VerticalAlignment="Top" Margin="0,210,169,0" FontSize="32" /> <TextBlock HorizontalAlignment="Left" Text="性别" Margin="344,278,0,279" FontSize="32" /> <TextBlock Text="出生日期" Margin="204,278,112,279" FontSize="32" /> <TextBlock Text="工号" Margin="6,278,330,279" FontSize="32" /> <TextBlock Text="名字" Margin="0,278,260,279" HorizontalAlignment="Right" FontSize="32" /> <ListBox VerticalAlignment="Top" ItemsSource="{Binding Path=Employees,Source={StaticResource departmentView}}" ItemTemplate="{StaticResource dtEmployees}" Margin="12,334,-46,0" FontSize="32" />

【windows phone】CollectionViewSource的妙用