Filtering and Sorting of an ObservableCollection in ListView-Binding

故事扮演 提交于 2019-12-20 03:52:06

问题


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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!