ItemContainerGenerator.ContainerFromItem()返回null吗? [英] ItemContainerGenerator.ContainerFromItem() returns null?

查看:309
本文介绍了ItemContainerGenerator.ContainerFromItem()返回null吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些奇怪的行为,我似乎无法解决.当我遍历ListBox.ItemsSource属性中的项目时,我似乎无法获得容器?我期望看到返回的ListBoxItem,但我只会得到null.

I'm having a bit of weird behavior that I can't seem to work out. When I iterate through the items in my ListBox.ItemsSource property, I can't seem to get the container? I'm expecting to see a ListBoxItem returned, but I only get null.

有什么想法吗?

以下是我正在使用的一些代码:

Here's the bit of code I'm using:

this.lstResults.ItemsSource.ForEach(t =>
    {
        ListBoxItem lbi = this.lstResults.ItemContainerGenerator.ContainerFromItem(t) as ListBoxItem;

        if (lbi != null)
        {
            this.AddToolTip(lbi);
        }
    });

ItemsSource当前设置为字典,并且确实包含许多KVP.

The ItemsSource is currently set to a Dictionary and does contain a number of KVPs.

推荐答案

最后解决了问题...通过将VirtualizingStackPanel.IsVirtualizing="False"添加到我的XAML中,现在一切都按预期工作.

Finally sorted out the problem... By adding VirtualizingStackPanel.IsVirtualizing="False" into my XAML, everything now works as expected.

不利的一面是,我错过了虚拟化带来的所有性能优势,因此我将负载路由更改为异步,并在加载时在列表框中添加了旋转器" ...

On the downside, I miss out on all the performance benefitst of the virtualization, so I changed my load routing to async and added a "spinner" into my listbox while it loads...

这篇关于ItemContainerGenerator.ContainerFromItem()返回null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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