WPF Course, Data Sorting and Filtering with CollectionView

Windows Presentation Foundation (WPF) is Microsoft’s UI framework widely used for developing desktop applications. WPF provides various data binding techniques. In this chapter, we will take a closer look at one of the key concepts in WPF, CollectionView, and explain how to sort and filter data.

1. What is CollectionView?

A CollectionView is an object used with WPF’s data binding functionality that provides a view of a data source. It manages the top-level collection and supports functionalities such as sorting, filtering, and grouping the contents of this collection. This allows the logical implementation of WPF’s MVVM pattern and enables more flexible handling of data in the UI.

2. Key Features of CollectionView

  • Sorting: A CollectionView can sort items in the collection based on specific properties.
  • Filtering: You can select only the items that meet certain conditions to display in the collection.
  • Grouping: Items can be grouped based on specific properties of the data.

3. Creating a CollectionView

Creating a CollectionView is very simple. First, define the data to be used with the CollectionView, and then create the CollectionView using CollectionViewSource. Below is a simple example.


using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public List<Person> People { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            People = new List<Person>()
            {
                new Person { Name = "John", Age = 30 },
                new Person { Name = "Jane", Age = 25 },
                new Person { Name = "Mike", Age = 35 },
            };

            CollectionViewSource collectionViewSource = new CollectionViewSource();
            collectionViewSource.Source = People;
            this.DataContext = collectionViewSource.View;
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

In the example above, we created a CollectionViewSource.

4. Sorting Data

To sort data in a CollectionView, you must use the SortDescriptions property. Below is an example of sorting by age.


collectionViewSource.SortDescriptions.Add(new SortDescription("Age", ListSortDirection.Ascending);

This code sorts in ascending order based on the Age property. If you want to sort in descending order, use ListSortDirection.Descending.

5. Filtering Data

Filtering allows you to show only the items that meet specific conditions. To filter in the CollectionView, you need to define the Filter property. Below is an example of filtering to show only people aged 30 and over.


collectionViewSource.Filter += (s, e) => 
{
    if (e.Item is Person person)
    {
        e.Accepted = person.Age >= 30;
    }
};

6. Binding to the UI

After sorting or filtering the data, you can bind it to the UI for display. Below is how to set up the binding in XAML.



    
        
            
            
        
    

7. Conclusion

Utilizing CollectionView in WPF allows for flexible sorting, filtering, and grouping of data sources. This plays a very important role in the implementation of the MVVM pattern. When developing applications using WPF, effectively employing these techniques can lead to more elegant and functional UIs.

8. References