如何使用带有 CollectionViewSource 的增量加载集合来对 UWP 中的列表视图项进行分组?
How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?
我跟进了 IncrementalLoadingCollection 的这个示例,并创建了一个具有增量加载集合(无限滚动)的应用程序。
XAML:
<ListView x:Name="PeopleListView">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<TextBlock Text="{x:Bind Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#:
public class Person
{
public string Name { get; set; }
}
public class PeopleSource : IIncrementalSource<Person>
{
private readonly List<Person> people;
public PeopleSource()
{
// Populate people
...
}
public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
{
// Gets and returns items from the collection according to pageIndex and pageSize parameters
...
}
}
var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
PeopleListView.ItemsSource = collection;
但现在我还需要对项目进行分组,例如按名称的长度分组。
另外,使用 CollectionViewSource 这个例子,我创建了一个我期望的例子:
XAML:
<Grid.Resources>
<CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="true" />
</Grid.Resources>
<ListView x:Name="PeopleListView" ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">
...
</ListView>
C#:
public string Name { get; set; }
// Populate people
...
var result =
from t in people
group t by t.Name.Length into g
orderby g.Key
select g;
groupInfoCVS.Source = result;
问题是我需要使用增量加载并对项目进行分组,我找不到将 IncrementalLoadingCollection 与 CollectionViewSource 一起使用的方法。
有谁知道如何做到这一点或可以提出解决方案?
感激不尽。
How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?
我不得不说,IncrementalLoadingCollection
不适用于 CollectionViewSource
。它们都需要直接设置为 ListView 的 ItemsSource 才能工作。因为他们需要检测listview Interaction
.
因此,根据您的要求,我们建议收听列表视图 ViewChanged
并手动添加更多项目。
例如
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;
}
}
};
}
我跟进了 IncrementalLoadingCollection 的这个示例,并创建了一个具有增量加载集合(无限滚动)的应用程序。
XAML:
<ListView x:Name="PeopleListView">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Person">
<TextBlock Text="{x:Bind Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#:
public class Person
{
public string Name { get; set; }
}
public class PeopleSource : IIncrementalSource<Person>
{
private readonly List<Person> people;
public PeopleSource()
{
// Populate people
...
}
public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
{
// Gets and returns items from the collection according to pageIndex and pageSize parameters
...
}
}
var collection = new IncrementalLoadingCollection<PeopleSource, Person>();
PeopleListView.ItemsSource = collection;
但现在我还需要对项目进行分组,例如按名称的长度分组。 另外,使用 CollectionViewSource 这个例子,我创建了一个我期望的例子:
XAML:
<Grid.Resources>
<CollectionViewSource x:Name="groupInfoCVS" IsSourceGrouped="true" />
</Grid.Resources>
<ListView x:Name="PeopleListView" ItemsSource="{Binding Source={StaticResource groupInfoCVS}}">
...
</ListView>
C#:
public string Name { get; set; }
// Populate people
...
var result =
from t in people
group t by t.Name.Length into g
orderby g.Key
select g;
groupInfoCVS.Source = result;
问题是我需要使用增量加载并对项目进行分组,我找不到将 IncrementalLoadingCollection 与 CollectionViewSource 一起使用的方法。 有谁知道如何做到这一点或可以提出解决方案? 感激不尽。
How to use Incremental Loading Collection with CollectionViewSource to group list view items in UWP?
我不得不说,IncrementalLoadingCollection
不适用于 CollectionViewSource
。它们都需要直接设置为 ListView 的 ItemsSource 才能工作。因为他们需要检测listview Interaction
.
因此,根据您的要求,我们建议收听列表视图 ViewChanged
并手动添加更多项目。
例如
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;
}
}
};
}