即使列表框有项目,ListBox ItemContainerGenerator也会返回null

发布于 2024-11-02 08:01:32 字数 1337 浏览 9 评论 0原文

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

我读了一些博客,发现这可能是因为垂直化堆栈面板。如果我禁用它,那么它可能会起作用,但就我而言,它会使列表框变得太重并且效率较低。

以下是列表框的代码:

 <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为空时它有项目。

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

还有一件非常值得注意的事情: 我看到 ItemsControl 中有 8 个列表框项目。 ItemContainerGenerator 状态显示 *Container generated *

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

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.

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.

Here is the code for listbox:

 <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">

This returns 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);

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-11-09 08:01:32

虚拟化意味着列表框项目仅在可见时才会生成。因此,您不能使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文