计算 ListBox 中可见项目的数量 [英] Calculating number of visible items in ListBox

查看:29
本文介绍了计算 ListBox 中可见项目的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一门课叫书;

class Book
{
    public string Name { get; set; }
    public string Author { get; set; }
    public int PagesCount { get; set; }
    public int Category { get; set; }
}

ListBox 显示书籍列表,并且 ItemTemplate 已被修改,以便直观地表示书籍.文字显示书名、作者和页数.然而,类别由某种颜色表示(例如,历史是蓝色,浪漫是红色等).现在,文本具有 OuterGlowBitmap 效果和从类别 (int) 到适当颜色的值转换器.ListBoxItem 的所有内容都绑定在 DataTemplate 中.从技术上讲,一切正常.

The ListBox displays a list of Books and the ItemTemplate has been modified so as to visually represent the Book. The text shows the book's Name, author and the number of pages. The category, however is represented by a certain color (for example, history is blue, romance is red etc.) Now, the text has an OuterGlowBitmap Effect and a value converter from the Category (int) to the appropriate Color. Everything is bound in DataTemplate for ListBoxItem. Technically, everything works fine.

然而,问题在于性能.似乎 outerGlow 位图效果在处理器上很重,所以当我有一个大约 500 本书的列表时,从数据库中检索数据大约需要 500 毫秒,但将项目实际加载到 ListBox 大约需要 10 秒.即使加载完成,滚动也很慢.我尝试将 VirtualizingStackPanel.IsVirtualizing 设置为 True,但无济于事.(在任何给定时间可以在数据库中的最大书籍数量约为 30000.)

The problem, however, is performance. It seems that the outerGlow bitmap effect is heavy on processor, so when I have a list of about 500 books, it takes about 500ms to retreive the data from the database but around 10 seconds to actually load the items into the ListBox. And even when the loading is done, scrolling is very laggy. I've tried to set the VirtualizingStackPanel.IsVirtualizing to True, but to no avail. (The maximum number of books that can be in the database at any given time is about 30000.)

但是,即使列表框中有 100 多个项目,人脑也无法快速处理,因此我不打算加载并向用户列出所有搜索到的书籍.这就是为什么我创建了一个包装导航类 BookNavigator,它实际上将列表框绑定到它的 ObservableCollection 对象.所有书籍都加载到此 BookNavigator 中,但只有 X 本书显示在列表框中(通过将它们添加到 observableCollection 中).

However, even when there is more than 100 items in the listbox, human mind can't process that much quickly, so I don't aim to load and list to the user all the books that are searched for. That is why I have created a wrapper navigation class BookNavigator that actually binds the listbox to its ObservableCollection object. All the books are loaded into this BookNavigator, but only X of them are displayed in the listbox (by adding them to the observableCollection).

这样做的问题是我希望显示的书籍数量足够小,以便列表框不显示滚动条,所以我可以实现自己的滚动方法(First、Previous、Next、Last,或者只是我自己的滚动条,没关系).

The problem with this is that I want that number of displayed books to be small enough for listbox not to display the scrollbar, so i can implement my own methods of scrolling (First, Previous, Next, Last, or simply my own scrollbar, doesn't matter).

如何计算要显示的项目数以使滚动条不显示?

How can I calculate how many items to display so the scrollbar is not shown?

弹出的两个问题: - 调整应用程序的大小可以改变列表框的大小- 并非所有列表框项的高度都相同(取决于作者的数量).

Two problems that pop up: - Resizing the application can change the listbox's size - Not all the listbox items are of the same height (depending on the number of authors).

有什么方法可以实现我想要做的吗?

Is there any way to achieve what I am trying to do?

编辑(作为对 Martin Harris 的回复)

Martin Harris 建议的代码的问题是 foreach 循环使用 FrameworkElement,但列表框填充了 Book 类型的对象,这些对象不是从 FrameworkElement 继承的,也没有任何其他计算其高度的方法.ListBoxItem 的根元素是一个网格,所以也许可以检索这个网格,但我不知道该怎么做?

The problem with the code Martin Harris suggested is that the foreach loop uses FrameworkElement, but the listbox is filled with objects of type Book which does not inherit from FrameworkElement, nor it has any other mean of calculating its height. The ListBoxItem's root element is a grid, so maybe it would be possible to retreive this grid, but I don't know how to do that?

有什么方法可以获取实际创建的 UI 元素来表示列表框项?

Is there any way at all to get the actual UI elements that are created to represent the listbox item?

编辑

我发现这个 Q/A 似乎是我需要的..ItemContainerGenerator

I found this Q/A which seems to be what I need.. ItemContainerGenerator

推荐答案

可以在这里隐式找到解决方案:解决方案

The solution can implicitly be found here: Solution

这篇关于计算 ListBox 中可见项目的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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