So I have a datagrid that I need to add custom sorting for and I also need to know the exact order of the sort.
I have read in order to do this I need to implement a custom icollectionview and bind it to the datagrid.
The problem I am having is that the documentation Microsoft gives on this interface is not that great. Does anyone know how to do this or have any good tutorials on how to implement this interface for silverlight?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I’m looking for the same, and found this article from Colin Eberhardt. It shows how to implement sorting using an implementation of ICollectionView
If you figure out how to implement filtering, please let me know.
Method 2
Silverlight 3 now supports and implementation of the ICollectionView, called PagedCollectionView.
This provides sorting and grouping, but not filtering.
Method 3
The best example I’ve found is Microsoft’s ICollectionView implementation that was created for use with the DataGrid. Unfortunately, they tagged it internal so you can’t just use it outright (and a copy & paste of the source requires a few modifications). Bust out Reflector and open System.Windows.Controls.Data.dll – navigate to the System.Windows.Controls namespace and there you can find ListCollectionView. Here’s the definition to show that it implements ICollectionView:
internal class ListCollectionView : ICollectionView, INotifyCollectionChanged, INotifyPropertyChanged, IEnumerable
…
I really wish MS would provide this class – practically every LOB app needs it.
Method 4
Here’s how you perform a sort using ICollectionView.
ICollectionView view = CollectionViewSource.GetDefaultView(someCollection);
view.SortDescriptions.Add(new SortDescription("someProperty", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("someOtherProperty", ListSortDirection.Descending));
However it’s not exactly what I would call “custom sorting”… It just lets you choose the sort criteria and direction. Could you be more specific on what you want to do ?
Method 5
For other’s who browse to this question, I’ve found these sites helpful as well:
- Silverlight 3 Custom Sorting with Paging Support – Manish Dalal’s blog
- ICollectionView explained « C# Disciples
I hope that Silverlight 5 has a better alternative. 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0