如何知道何时出现ListBoxItem? [英] How to know when a ListBoxItem comes into view?

查看:60
本文介绍了如何知道何时出现ListBoxItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个事件告诉我何时出现列表框项目?

Is there an event that tells me when a list box item comes into view?

我遇到的问题是我可以将几千个元素设置为我的ListBox.ItemSource.每个元素都会生成一个位图(需要一些时间),因此,如果我只是将此位图生成放入构造函数中,则创建该集合将需要花费很多时间.相反,我想在项目进入视图时推迟位图的生成.

The problem I have is that I can have several thousand elements that I set as my ListBox.ItemSource. Each element will generate a bitmap (which takes a while) so if I would just put this bitmap generation in the constructor creating the collection would take forever to create. Instead I want to defer the bitmap generation when an item comes into view.

有没有办法做到这一点?理想情况下,我宁愿不遍历所有项目并检查它们是否可见.

Is there a way to do this? Ideally I would prefer not to loop through all the items and check if they are visible.

推荐答案

是否有一个事件告诉我何时出现列表框项目?

Is there an event that tells me when a list box item comes into view?

您可以处理ListBoxItem容器的Loaded事件:

You could handle the Loaded event of the ListBoxItem container:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <EventSetter Event="Loaded" Handler="OnItemLoaded" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>


private void OnItemLoaded(object sender, RoutedEventArgs e)
{
    ListBoxItem lbi = sender as ListBoxItem;
    object dataItem = lbi.DataContext;
    //...
}

这篇关于如何知道何时出现ListBoxItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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