码迷,mamicode.com
首页 > 其他好文 > 详细

CollectionView

时间:2015-04-23 21:17:05      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

 
  
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     }

 

CollectionView

标签:

原文地址:http://www.cnblogs.com/changbaishan/p/4451572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!