CollectionView

 
  
2 <Window x:Class="MainWindow"
3 Loaded="Window_Loaded">
4
5 <ListBox x:Name="lstPeople" Width="200">
6             <ListBox.ItemTemplate>
7                 <DataTemplate>
8 <StackPanel>
9                         <StackPanel Orientation="Horizontal">
10                             <TextBlock Text="{Binding LastName}" />
11                             <TextBlock Text=", " />
12                             <TextBlock Text="{Binding FirstName}" />
13                         </StackPanel>
14                         <TextBlock Text="{Binding State}" />
15                         <TextBlock Text="{Binding Dateofbirth}" />
16                     </StackPanel>            
17                 </DataTemplate>
18             </ListBox.ItemTemplate>
19         </ListBox>
20          
21         <StackPanel Orientation="Horizontal" Grid.Row="1">
22             <Button x:Name="btnSortbylast" Content="Sort By LastName" Click="btnSortbylast_Click" />
23             <Button x:Name="btnSortbydob" Content="Sort By DOB" Click="btnSortbydob_Click" />
24             <Button x:Name="btnFilter" Content="Show Smiths" Click="btnFilter_Click" />
25         </StackPanel>
 
 
1         private Collection<Person> people = new Collection<Person>();
2         private bool isfiltered = false;
3        
4         private void btnSortbylast_Click(object sender, RoutedEventArgs e)
5         {
6             //CollectionView, 表示用于分组、排序、筛选和导航数据集合的视图。
7             CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
8             cv.SortDescriptions.Clear();
9             cv.SortDescriptions.Add(new SortDescription("LastName", ListSortDirection.Ascending));
10         }
11
12         private void btnSortbydob_Click(object sender, RoutedEventArgs e)
13         {
14             CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
15             cv.SortDescriptions.Clear();
16             cv.SortDescriptions.Add(new SortDescription("Dateofbirth", ListSortDirection.Descending));
17         }
18
19         private void btnFilter_Click(object sender, RoutedEventArgs e)
20         {
21             CollectionView cv = (CollectionView)CollectionViewSource.GetDefaultView(lstPeople.ItemsSource);
22             if(isfiltered)
23             {
24                 cv.Filter=null;
25                 isfiltered=false;
26             }else{
27                 cv.Filter = new Predicate<object>(FilterBySmith);
28                 isfiltered=true;
29             }
30         }
31        
32         private bool FilterBySmith(object item)
33         {
34             Person p = item as Person;
35             return p.LastName.Trim()=="Smith";
36         }
37
38         private void Window_Loaded(object sender, RoutedEventArgs e)
39         {
40             people.Add(new Person() {FirstName="Tom", LastName="Smith", State="NY", Dateofbirth=new DateTime(1962, 10, 30)});
41             people.Add(new Person() { FirstName = "John", LastName = "Doe", State = "CA", Dateofbirth = new DateTime(1970, 3, 20) });
42             people.Add(new Person() { FirstName = "Jane", LastName = "Doe", State = "AL", Dateofbirth = new DateTime(1970, 3, 20) });
43             people.Add(new Person() { FirstName = "Bill", LastName = "Johnson", State = "CA", Dateofbirth = new DateTime(1970, 3, 20) });
44             people.Add(new Person() { FirstName = "Stacey", LastName = "Zany", State = "GA", Dateofbirth = new DateTime(1970, 3, 20) });
45             people.Add(new Person() { FirstName = "Liz", LastName = "Smith", State = "TX", Dateofbirth = new DateTime(1970, 3, 20) });
46             people.Add(new Person() { FirstName = "Jim", LastName = "Jones", State = "TX", Dateofbirth = new DateTime(1970, 3, 20) });
47            
48             lstPeople.ItemsSource=people;
49         }
50     }
51    
52     public class Person
53     {
54         public string FirstName { get; set; }
55         public string LastName { get; set; }
56         public string State { get; set; }
57         public DateTime Dateofbirth { get; set; }
58     }

 

posted @   长白山  阅读(201)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示