ListBox ItemContainerGenerator返回null,即使listbox中有项目也是如此 [英] ListBox ItemContainerGenerator returns null even when listbox has items

查看:249
本文介绍了ListBox ItemContainerGenerator返回null,即使listbox中有项目也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几个项目的列表框,直到没有滚动条出现时一切都很好,但是一旦滚动条出现,ItemContainerGenerator将返回null.

I've a listbox with couple of items, till the time no scrollbar appears everything goes perfectly fine but once scrollbar comes then ItemContainerGenerator returns null.

我阅读了一些博客,发现可能是由于verticalization stackpanel所致.如果我禁用了该功能,则它可能会起作用,但就我而言,它会使列表框变得过于沉重且效率较低.

I read some blogs and found it may be because of vertualization stackpanel. If i disable that then it may work but in my case it would make listbox too much heavy and less efficient.

这是列表框的代码:

 <ListBox x:Name="EventListBox"
          DockPanel.Dock="Top"
          Margin="5,5,5,5"
          FocusVisualStyle="{x:Null}"
          ItemsSource="{Binding EventModelViewCollectionView}"
          Style="{StaticResource DefaultListBoxStyle}"
          ItemTemplate="{StaticResource EventTemplate}"
          SelectedItem="{Binding EventModelViewCollection.SelectedItem}"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          ScrollViewer.CanContentScroll="True"
          ScrollViewer.IsDeferredScrollingEnabled="False"
          ScrollViewer.ScrollChanged="EventScrollViewer_ScrollChanged"
          IsSynchronizedWithCurrentItem="True"
          Loaded="ListBox_Unloaded">

这将返回null.

在这里firstContainer应该是第一个列表框元素.我验证了itemsControl正确,并且当firstContainer为null时,它具有项目.

Here firstContainer should be the first listbox element. I verified that itemsControl is correct and it has items when firstContainer is null.

DependencyObject firstContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(0);

另一件值得注意的事情: 我看到ItemsControl中有8个列表框项. ItemContainerGenerator状态为* Container Generated *

One more quite noticing thing: I saw there're 8 listbox items in ItemsControl. ItemContainerGenerator status says *Container Generated *

不知道为什么不返回ItemsContainer 想法??

No idea why it's not returning ItemsContainer Thoughts ??

推荐答案

虚拟化意味着仅在可见时才生成listboxitems.因此,您不能使用ItemContainerGenerator来获取未生成的(不可见的)列表框项目.

Virtualization means that listboxitems only gets generated if visible. So you can not use the ItemContainerGenerator to get non generated (non visible) listbox items.

您真的需要直接使用列表框项吗?看起来您的列表框是数据驱动的,因此可以直接使用数据.

Do you really need the listbox item directly? It looks like your listbox is data driven so work with the data directly.

这篇关于ListBox ItemContainerGenerator返回null,即使listbox中有项目也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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