2

In earlier Versions of .net you could use something like

ICollectionView collectionView = CollectionViewSource.GetDefaultView(AllImages);

To build up filter mechanisms for UI-Elements. But this doesn't seem to exist in WinRT anymore.

I'm using a ListBox-Element to display an User-List. The User-Class contains the properties username (String), isOnline (Boolean), isFriend (Boolean) and unreadMails (Int). I need some kind of Filtering and sorting.

Filter by:

  • no Filter
  • only isOnline == true

Order by:

  • unreadMails > 0
  • unreadMails == 0
    • AND isOnline == true
      • AND isFriend == true
      • isFriend == false
    • AND isOnline == false
      • AND isFriend == true
      • AND isFriend == false

The properties may change while running, it would be cool if the list updates itself automatically. (Binding is already working, only the filter and order is missing).

Any Ideas how to achieve this?

max06
  • 79
  • 2
  • 7
  • Take a loot at [http://msdn.microsoft.com/en-us/library/ff407126.aspx](http://msdn.microsoft.com/en-us/library/ff407126.aspx) – Jordy van Eijk Feb 19 '13 at 12:05
  • Sadly, there's no GetDefaultView-Function available in the CollectionViewSource-Class... – max06 Feb 19 '13 at 12:10
  • Here's an implementation of CollectionView for WinRT: http://www.codeproject.com/Articles/527686/A-WinRT-CollectionView-class-with-Filtering-and-So – Richard Cook Feb 26 '13 at 06:34

2 Answers2

2

As you'd figured out the CollectionViewSource doesn't have support for filters or sorts in WinRT/XAML. Your solution is then to sort the contents of the associated ObservableCollection.

Filip Skakun
  • 31,347
  • 6
  • 71
  • 99
  • Any quick ideas for an easy sorting of the Collection by more than one parameter? At the moment, I'm using `ObservableCollection _sort = new ObservableCollection(this.OrderBy(User => User.Nickname));` to create a temp Collection. After that I compare each element with the original list and move the items to their correct new position. (Needed for UX Transitions). But that's only one sort parameter... – max06 Feb 20 '13 at 11:28
  • You can use `OrderBy` for first column and `ThenBy` for the following. See http://stackoverflow.com/questions/298725/multiple-order-by-in-linq. – Filip Skakun Feb 20 '13 at 15:33
  • I'm using now Query expressions. 5 different Expressions for splitting up the whole list, then appending all 5 objects to a new Collection. At least, sorting the original one to the exact order of the temp Collection -> perfect! – max06 Feb 20 '13 at 16:07
1

There is a custom ICollectionView implementation available on CodeProject that adds the functionality of sorting/filtering. Here is a link to the article/code: http://www.codeproject.com/Articles/527686/A-WinRT-CollectionView-class-with-Filtering-and-So

This will bring back some of the functionality that is missing in the WinRT version of ICollectionView. Hopefully this will help someone who comes across this question as I did, looking for the ability to filter a collection.

dub stylee
  • 3,012
  • 5
  • 33
  • 57