树视图上的 VirtualizingStackPanel 不是虚拟化
我在这里遇到一个问题,我想在 TreeView 中显示一些项目,大约 100.000 个元素。 如果我使用默认的 WPF TreeView,一切似乎都可以工作,但如果我使用自定义 TreeView(目前只是一个 ItemsControl),虚拟化似乎不再工作。 在网上研究时,我尝试了一些解决方案,但似乎都不起作用...... 这是我的 xaml:
<Style TargetType="{x:Type my:MultiSelectionTreeView}">
<Setter Property="TreeView.Background" Value="Transparent"/>
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
<Setter Property="TreeView.OverridesDefaultStyle" Value="True" />
<Setter Property="ItemsControl.ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="TreeView.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:MultiSelectionTreeView}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Focusable="True" CanContentScroll="true"
Padding="4"
VerticalScrollBarVisibility="Auto">
<ItemsPresenter HorizontalAlignment="Stretch"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这些项目位于带有 DataBinding 的 ObservableCollection 中,所以这不应该是问题......但它是什么???
问候,
于尔根
I've got a problem here, I want to show some items in a TreeView, about 100.000 Elements.
If i use the default WPF TreeView everything seems to work, but if i use a custom-TreeView (which is at the moment only an ItemsControl), virtualization doesn't seem to work anymore.
While researching over the web, I've tried some solutions but none of them seems to work...
Here's my xaml:
<Style TargetType="{x:Type my:MultiSelectionTreeView}">
<Setter Property="TreeView.Background" Value="Transparent"/>
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
<Setter Property="TreeView.OverridesDefaultStyle" Value="True" />
<Setter Property="ItemsControl.ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="TreeView.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type my:MultiSelectionTreeView}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer Focusable="True" CanContentScroll="true"
Padding="4"
VerticalScrollBarVisibility="Auto">
<ItemsPresenter HorizontalAlignment="Stretch"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The Items are in an ObservableCollection with DataBinding, so that shouldn't be the Problem... but what is it???
Greets,
Jürgen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
VirtualizingStackPanel 有一些特殊的代码来查找 TreeView 和 TreeViewItem。此外,TreeViewItem 实现了 VirtualizingStackPanel.IProvideStackingSize,这是您无法实现的内部接口。
因此,如果您尝试复制 TreeView 中的项目的层次结构,那么您必须从 TreeView 派生才能使用虚拟化(而不是 ItemsControl)。
The VirtualizingStackPanel has some special code that looks for TreeView and TreeViewItem. In addition, TreeViewItem implements
VirtualizingStackPanel.IProvideStackingSize
, which is a internal interface which you won't be able to implement.So if you are trying to replicate the hierarchical structure of items like in the TreeView, then you'd have to derive from TreeView to use virtualization (not ItemsControl).