C# uwp限制AdvancedCollectionView中的项目数
我有两个来自windows community toolkit的高级集合视图,它们都使用不同的过滤器和排序绑定到同一个ObservableCollection,基本上在其中一个视图中,我只需要显示最近的和数量有限的项。我怎样才能做到这一点C# uwp限制AdvancedCollectionView中的项目数,c#,xaml,uwp,windows-community-toolkit,C#,Xaml,Uwp,Windows Community Toolkit,我有两个来自windows community toolkit的高级集合视图,它们都使用不同的过滤器和排序绑定到同一个ObservableCollection,基本上在其中一个视图中,我只需要显示最近的和数量有限的项。我怎样才能做到这一点 PeoplePrivate = new ObservableCollection<Person>(); var People = new AdvancedCollectionView(PeoplePrivate, true) {
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));
PeoplePrivate=newobservedcollection();
var People=newadvancedcollectionview(PeoplePrivate,true){Filter=x=>true};
添加(新的SortDescription(nameof(Person.Name),SortDirection.升序));
var RecentPeople=newadvancedcollectionview(PeoplePrivate,true){Filter=x=>true};
RecentPeople.SortDescriptions.Add(新的SortDescription(name of(Person.Modified)、SortDirection.Descending));
正如您在上面的代码中所看到的,根据修改日期,recentPeople应该只显示最近的20个人。似乎没有任何属性可以在advancedCollection视图上设置最大大小或执行“Take(20)”之类的操作
我试图通过创建一个带有Take(20)的IEnumerable first来返回一个新的advancedCollection,但这看起来并不正确,因为我需要将它链接到同一个ObservableCollection
查看或执行类似于“Take(20)”的操作我试图通过使用Take(20)创建IEnumeralbe来返回一个新的advancedCollection
目前,AdvancedCollectionView
尚未提供此方法来获取最近的数字项目。但您可以删除源代码中除前20项之外的所有项
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);
}
}
用法
RecentPeople.GetTopRang(20);
我喜欢提供的WPF答案,并在集合视图绑定到ListView时使用绑定转换器切掉集合视图的最终结果。那么它应该在集合更改时更新并重新筛选?这只在我调用此扩展方法时有效,它不会自动同步数据(如filter或sortYep),您需要在数据排序后调用此方法。所以我创建了这个扩展方法。这很有帮助,但不是我想要的答案,谢谢,尽管这将是一个有趣的特性。我也在为另一个用例的ObservableCollection设计一个不同的包装器,但这项工作的一部分也可以服务于这个用例。你能在这里提交一个关于工具包回购的问题吗?