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     }

时间: 2024-08-13 11:31:43

CollectionView的相关文章

SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

#import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+WebCache.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> // 数据源 @property

iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html #import "ViewController.h" #import <ImageIO/ImageIO.h> #import "UIImageView+WebCache.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICo

Dynamic支持CollectionView布局 、 MotionEffects特效 、 BlurImage效果 、 TextKit

1 使用UIDynamicAnimator对集合视图进行布局 1.1 问题 UIKit Dynamic动力模型一个非常有趣的用途就是影响集合视图的布局,可以给集合视图的布局添加各种动力行为,使其产生丰富多彩的效果,本案例使用UIDynamicAnimator对集合视图进行布局,实现一个弹性列表,如图-1所示: 图-1 1.2 方案 首先创建一个SingleViewApplication项目,给UIColor类创建一个分类UIColor+RandomColor,提供一个产生随机颜色的静态方法ran

iOS9 collectionView新特性

近日因为系统升级导致xcode6.系列版本出现bug,于是开始使用xcode7.在使用之余突然想到collectionView在iOS9中发布了一个可以移动cell的新特性,就尝试着将其实现,无奈api文档接口无法查看,只有一些列的api放在那里.于是上网查找,发现国内没有搜索到此类文章,于是FQ继续找,最终找到的竟然都是swift版本,于是将其转换为oc版本以帮助国内需要的朋友学习使用.下面是具体用法: 1.创建collectionView并设置代理 - (UICollectionView *

ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 前言 现在市面上有很多 app 都有联动功能,有的是两个 TableView 之间的联动,比如美团外卖,百度外卖,饿了么等等.有的是 TableView 与 CollectionView 之间的联动

CollectionView垂直缩放卡片布局

实现效果 实现思路 从效果图可以看到变化是,越是往中间滚动的item显示最大,越显眼.而越是往前面,或者越是后面的,反而显示越小,这样就形成了视觉差. 实现的思路就是通过重写在可见范围内的所有item的方法: 1 2 3 - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect 通过这个API可以获取到原始属性,然后利用公式来计算缩放. 难点 如何计算缩放系数.

iOS基础之CollectionView(集合视图)

在iOS6.0之后,苹果推出了?个新的继承于UIScrolleriew的一个视 图,UICollectionView,也被称之为集合视图.和UITableView共同作为 在开发中常常用的两个视图,常常作为项目的主界面出现. 代码演示: #import "YourCollectionViewCell.h" @implementation YourCollectionViewCell -(instancetype)initWithFrame:(CGRect)frame{ self = [

CollectionView 简用

创建一个CollectionView 分为几个步骤 1.先创建布局FlowLayout 设置布局格式 2.创建CollectionView 并使用布局Flowlayout  -initWithFrame: collectionViewLayout: 3.遵循协议delegate dataSource 4.注册cell 5.添加到view #import "ViewController.h"#import "reusableview.h" - (void)viewDi

collectionView 防止cell复用的方法

collectionView 防止cell复用的方法 一: //在创建collectionView的时候注册cell(一个分区) UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; for (UIView *view in cell.contentView.subviews) { [view removeFro

collectionView布局

关于 collectionView的layout布局方法: 设置cell的间距,行间距,组与组之间的间距,都是在layout里面来设置. 包括,滚动方向. -(void)prepareLayout [super prepareLayout] //最小行间距 self.minimumLineSpacing =1; //最小cell间距 self.minimumInteritemSpacing =1; //组与组之间的间距 self.sectionInset =UIEdgeInsetsMake(0,