views:

2253

answers:

5

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?

A: 

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 ?

Thomas Levesque
the problem I have here is that silverlight doesn't actually have CollectionViewSource so I am going to have to implement it
zachary
OK, I didn't realize this class was missing in Silverlight. What's strange is that the ICollectionView interface is present, but there doesn't seem to be any class that implements it... even the ItemCollection class doesn't (in WPF it does). Weird...
Thomas Levesque
Yes weird... and annoying. I could really use an implementation of this class.
James Cadd
+1  A: 

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.

James Cadd
+2  A: 

Hi! 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.

Larsi
I was able to get the code in the article working to sort my grid.
zachary
A: 

Check System.Windows.Data in SL 3. I don't know about prior versions.

Ryan
A: 

Silverlight 3 now supports and implementation of the ICollectionView, called PagedCollectionView.

This provides sorting and grouping, but not filtering.

Phillip Ngan