计算列表框中的可见项数 [英] Calculating number of visible items in ListBox

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

问题描述

我有一个叫做Book的课;

I have a class called Book;

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

ListBox显示一个Book列表,并且ItemTemplate已修改,以便直观地表示Book.文字显示该书的名称,作者和页数.但是,类别用某种颜色表示(例如,历史记录是蓝色,浪漫史是红色等).现在,文本具有OuterGlowBitmap效果和从类别(整数)到适当颜色的值转换器.一切都绑定在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.

但是,问题在于性能.似乎externalGlow位图效果在处理器上很沉重,因此当我有大约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).

问题是我希望所显示的书籍数量足够小,以使列表框不显示滚动条,因此我可以实现自己的滚动方法(第一,上一个,下一个,最后一个"或仅是我自己的)滚动条,没关系).

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

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

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