When the binding source is a set, WPF actually uses CollectionView to bind.
The collection view is a layer at the top of the bound source set. You can use sorting, filtering, and grouping queries to navigate and display the source set without changing the basic source set itself. The set view also maintains a pointer to the current item in the set. If the source set implements the INotifyCollectionChanged interface, the changes caused by the CollectionChanged event will be propagated to the view.
In the code, you can obtain the ICollectionView interface through CollectionViewSource. GetDefaultView to operate the view.
For example
ICollectionView view = CollectionViewSource. GetDefaultView (
This. FindResource ("photos "));
The GetDefaultView parameter is an object, that is, the source.
Sort
ICollectionView view = CollectionViewSource. GetDefaultView (
This. FindResource ("photos "));
View. SortDescriptions. Add (new SortDescription (
PropertyName, ListSortDirection. Descending ));
SortDescriptions is a type of SortDescriptionCollection. It supports Add, Remove, Clear, and other Collection methods. You can add multiple SortDescription, just like the order by statement in SQL.
TIP:
Customize your own sorting rules:
If collection implements the IList interface (which is implemented by most collections in the system), you can convert ICollectionView to ListCollectionView. This class has a CustomSort attribute and is an IComparer interface, implement a class that inherits this interface and assign it to CustomSort.
Group
// Get the default view
ICollectionView view = CollectionViewSource. GetDefaultView (
This. FindResource ("photos "));
// Do the grouping
View. GroupDescriptions. Clear ();
View. GroupDescriptions. Add (new PropertyGroupDescription ("DateTime "));
Although the Code above implements grouping, it is still displayed on the interface and is not differentiated. We need to clearly separate each group and use ItemsControl. groupStyle: defines the ememplate to distinguish between groups.
<ListBox.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate> <Border BorderBrush="Black" BorderThickness="1"> <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/> </Border> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle></ListBox.GroupStyle>
Filter
ICollectionView view = CollectionViewSource. GetDefaultView (this. FindResource
("Photos "));
View. Filter = delegate (object o ){
Return (o as Photo). DateTime-DateTime. Now). Days <= 7;
};
Filter is a delegate, Predicate <object>. If the received object is a parameter, the bool value is returned.
Next
void previous_Click(object sender, RoutedEventArgs e){// Get the default viewICollectionView view = CollectionViewSource.GetDefaultView(this.FindResource(“photos”));// Move backwardview.MoveCurrentToPrevious();// Wrap around to the endif (view.IsCurrentBeforeFirst) view.MoveCurrentToLast();}void next_Click(object sender, RoutedEventArgs e){// Get the default viewICollectionView view = CollectionViewSource.GetDefaultView(this.FindResource(“photos”));// Move forwardview.MoveCurrentToNext();// Wrap around to the beginningif (view.IsCurrentAfterLast) view.MoveCurrentToFirst();}
Use a view in Xaml
SortDescriptions requires the following namespace: xmlns: componentModel = "clr-namespace: System. ComponentModel; assembly = WindowsBase"
Here, Filter is an event that needs to be implemented in the background code, unlike the above delegate
Void viewSource_Filter (object sender, FilterEventArgs e)
{
E. Accepted= ((E. ItemAs Photo). DateTime-DateTime. Now). Days <= 7;
}