Use of the Windows Phone collectionviewsource class

Source: Internet
Author: User
The CollectionViewSource class can generate views for data sources according to specified conditions, and can specify sorting, filtering, and even grouping. The relationship between the views and data sources it generates can be understood as the relationship between views and tables in a relational database.
1. Add a ListBox and three buttons to the View. (Remember to uncomment the DataTemplate ...)

 

2. The background code is as follows

 
 public class Item
    {
        public string Name {get; set;}
        public string Sex {get; set;}
        public int Age {get; set;}
    }
    public partial class MainPage: PhoneApplicationPage
    {
        // Constructor
        public MainPage ()
        {
            InitializeComponent ();
                ObservableCollection <Item> arr = new ObservableCollection <Item> ();
                Item item = new Item ();
                item.Name = "Lei";
                item.Sex = "Male";
                item.Age = 20;
                arr.Add (item);

                item = new Item ();
                item.Name = "Li Miao";
                item.Sex = "Female";
                item.Age = 10;
                arr.Add (item);

                item = new Item ();
                item.Name = "Li Ming";
                item.Sex = "Male";
                item.Age = 20;
                arr.Add (item);

                item = new Item ();
                item.Name = "Li Mingming";
                item.Sex = "Male";
                item.Age = 10;
                arr.Add (item);

                item = new Item ();
                item.Name = "Li Support";
                item.Sex = "Male";
                item.Age = 11;
                arr.Add (item);

                item = new Item ();
                item.Name = "Li Lai";
                item.Sex = "Male";
                item.Age = 11;
                arr.Add (item);

                _source = new CollectionViewSource ();
               // Initialize the data source of CollectionViewSource
                _source.Source = arr;
                // Data source bound to ListBox

// Note that the Listbox is bound to the View property of CollectionViewSource
                lb.ItemsSource = Source;
        }
        private CollectionViewSource _source;

        public ICollectionView Source
        {
            get {return _source.View;}
        }

        private void OnButtonAgeAscendingClick (object sender, RoutedEventArgs e)
        {
            // We prefer to sort in ascending age order. You need to specify the name of the attribute used for sorting. Of course, the Age attribute is used for sorting according to age.
            // Then specify the sorting method, ListSortDirection.Ascending, sort in ascending order.

            // Clear the filter conditions and display all data
             _source.Filter + = (o, arg) =>
            {
                arg.Accepted = true;
            };
            _source.SortDescriptions.Add (new SortDescription ("Age", ListSortDirection.Ascending));
        }

        private void OnButtonShowGirlClick (object sender, RoutedEventArgs e)
        {
            // Specify to show only girls
            // Accepted value indicates whether the item passed the filter
            _source.Filter + = (o, arg) =>
                {
                    arg.Accepted = ((Item) arg.Item) .Sex == "Female";
                };
        }

        private void OnButtonGroupClick (object sender, RoutedEventArgs e)
        {
            // Clear the filter conditions.
            _source.Filter + = (o, arg) =>
            {
                arg.Accepted = true;
            };
            // Group by age
            _source.GroupDescriptions.Add (new PropertyGroupDescription ("Age"));
        }

    }
The next article will explain: the use of CollectionViewSoure on LongListSelector.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.