问题
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
- AND isOnline == true
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/14956999/filtering-and-sorting-of-an-observablecollection-in-listview-binding