WPF如何从CollectionViewSource设置最大结果数 [英] WPF How to set a maximum amount of results from CollectionViewSource

查看:106
本文介绍了WPF如何从CollectionViewSource设置最大结果数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ItemsControl 中的 DataTemplate 显示一组数据,其中 ItemsPanel 设置为大小为3 x 3的统一网格。 ItemsControl ItemsSource $ c>设置为绑定到 CollectionViewSource ,后者根据搜索词过滤源集合。



我要绑定的列表是任意大小的,但是我只希望显示9个结果,但我终生不能这样做我自己想办法:

a)限制 CollectionViewSource 输出前9个项目

b)限制 UniformPanel 仅为3 x 3,并且从不创建新行

c)将 ItemsControl 限制为仅允许一次可以创建9个数据模板。



我真的很抓头,因为我敢肯定这是一个常见的数据绑定方案,但是我找不到任何东西

解决方案

我能想到的两个解决方案:



<如果使用的是View Model,请在其中放置逻辑以创建一个属性,可以绑定到只有9个元素的属性。确保已对它进行了单元测试,以确保超过9个元素永远不会潜入集合中。



或者,您可以使用转换器在绑定项源以将完整列表转换为前9个的简化列表时。


I am trying to display a set of data using a DataTemplate in an ItemsControl, with the ItemsPanel set as a uniform grid of size 3 x 3. The ItemsSource of the ItemsControl is set to bind to a CollectionViewSource which filters the source collection based on a search term. This all works fine.

The list I am binding to is of an arbitrary size, but I only want 9 results to be displayed, but I can't for the life of me work out how to either:
a) limit the CollectionViewSource to output the first 9 items
b) limit the UniformPanel to only 3 x 3 and never to create new rows
c) limit the ItemsControl to only allow 9 data templates to be created at once.

I'm really scratching my head because I'm sure this is a common databinding scenario but I can't find anything on the web about it.

解决方案

Two solutions I can think of:

If you are using View Model, put the logic in there to create a property that you can bind to that only ever has 9 elements. Make sure it's unit tested to ensure more than 9 elements never sneak into the collection.

Alternatively you could use a converter when binding the items source to convert the full list to a reduced list of the first 9.

这篇关于WPF如何从CollectionViewSource设置最大结果数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆