Home > Software engineering >  How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?
How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?

Time:09-25

I followed up this example of enter image description here

But now I also need to group the items, for example, group by length of the name. Separately, using this example of enter image description here

The problem is that I need to use Incremental Loading and group the items, I can't find a way to use IncrementalLoadingCollection with CollectionViewSource. Does anyone know how to do this or can suggest a solution? Grateful.

CodePudding user response:

How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?

I have to say, IncrementalLoadingCollection does not work for CollectionViewSource. Both of the them work that need to be set as ListView's ItemsSource directly. Because they need to detect listview Interaction.

So for your requirement, we suggest listen listview ViewChanged and add more item manually.

For example

private void PeopleListView_Loaded(object sender, RoutedEventArgs e)
{
    if (!(PeopleListView.ItemsPanelRoot is ItemsStackPanel itemsStackPanel)) return;
    var _itemsStackPanel = itemsStackPanel;
    var _scrollViewer = MyFindDataGridChildOfType<ScrollViewer>(PeopleListView);
    // This event handler loads more items when scrolling.
    _scrollViewer.ViewChanged  = (o, eventArgs) =>
    {
        if (eventArgs.IsIntermediate) return;
        double distanceFromBottom = itemsStackPanel.ActualHeight - _scrollViewer.VerticalOffset - _scrollViewer.ActualHeight;
        if (distanceFromBottom < 10) // 10 is an arbitrary number
        {
            for (int i = 1; i <= 50; i  )
            {
                var p = new Person { Name = "Person "   i };
                _people.Add(p);
                var result =
                            from t in _people
                            group t by t.Name.Length into g
                            orderby g.Key
                            select g;

                groupInfoCVS.Source = result;
                PeopleListView.ItemsSource = groupInfoCVS.View;
            }
        }
    };
}
  • Related