zoukankan      html  css  js  c++  java
  • WPF——CollectionView

     1  
    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 }



  • 相关阅读:
    歌德巴赫猜想
    Dice Possibility
    ACboy needs your help(简单DP)
    Bag of mice(概率DP)
    合唱队形(LIS)
    地震预测(模拟链表)
    关于KMP算法的感想
    Card Collector
    LOOPS
    Aeroplane chess(简单概率dp)
  • 原文地址:https://www.cnblogs.com/January/p/2434967.html
Copyright © 2011-2022 走看看