0

I have two advanced collection views from windows community toolkit and both of them are bound to same ObservableCollection with different filters and sorting, basically in one of them I need to show just the recent and limited number of items. How can I achieve that?

PeoplePrivate = new ObservableCollection<Person>();

        var People = new AdvancedCollectionView(PeoplePrivate, true) { Filter = x => true };
        People.SortDescriptions.Add(new SortDescription(nameof(Person.Name), SortDirection.Ascending));

        var RecentPeople = new AdvancedCollectionView(PeoplePrivate, true) { Filter = x => true };
        RecentPeople.SortDescriptions.Add(new SortDescription(nameof(Person.Modified), SortDirection.Descending));

As you can see in the code above recentPeople should only show recent 20 people according to the modified date. There doesn't seem to be any property to set max size on the advancedCollection view or do anything like "Take(20)".

I tried to return a new advancedCollection by creating a IEnumerable first with Take(20) but that doesn't look the right way because I need to keep it linked to the same ObservableCollection.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75
  • This would be an interesting feature. I've also been toying with a different wrapper for ObservableCollection for another use-case, but part of that work could also serve this use-case. Could you file an issue on the Toolkit repo here? https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/new – Michael Hawker - MSFT Apr 25 '19 at 17:31

2 Answers2

1

view or do anything like "Take(20)" I tried to return a new advancedCollection by creating a IEnumeralbe first with Take(20)

Currently AdvancedCollectionView has not provide this method to get recent number items. But you could remove all the items except top 20 of the source.

public static class AdvancedCollectionViewEx
{
    public static void GetTopRang(this AdvancedCollectionView acv, int Range)
    {
        do
        {
            var LastIndex = acv.Source.Count - 1;
            acv.Source.RemoveAt(LastIndex);

        } while (acv.Source.Count > Range);
    }
}

Usage

RecentPeople.GetTopRang(20);
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
0

I like the WPF answer provided here and use a Binding Converter to chop the end-result of the collection view when it's bound to the ListView. Then it should get updated when the collection changes and re-filter?

Michael Hawker - MSFT
  • 1,572
  • 12
  • 18