ItemContainerGenerator.ContainerFromItem() 返回 null? [英] ItemContainerGenerator.ContainerFromItem() returns 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 当前设置为 Dictionary 并且确实包含许多 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屋!