VirtualizingStackPanel 不工作
我有以下列表框:
<ScrollViewer>
<!--Spec Definitions-->
<ListBox DataContext="{Binding SpecPackageSpecGroupListViewModel}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True"
ItemContainerStyle="{StaticResource SpecPackageSpecGroupListBoxStyle}"
ItemsSource="{Binding SortedChildren}"
Background="Transparent"
BorderThickness="0" SelectionMode="Extended"
Margin="5,5,5,5">
<ListBox.ItemTemplate>
<DataTemplate>
<Controls:SpecPackageSpecGroupControl/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
该列表框应该容纳约 1000 个项目,但项目很复杂。 我希望它能够与 VirtualizingStackPanel 一起使用,因此我将可视化 XAML 配置设置为:
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
我的问题是我认为它不起作用 - 首先 - 加载 ~700 个项目需要很长时间,其次,当我在我的控件构造函数上打断点 - 我可以看到它被调用了 700 次:
public static int Counter = 0;
public SpecPackageSpecGroupControl()
{
InitializeComponent();
Counter++;
if (Counter%100 == 0)
Console.WriteLine("Hi");
}
我在 Console.WriteLine("Hi") 上断点,我可以看到静态计数器达到了 700。
所以基本上 UIElements 正在被创建,尽管这是虚拟模式。
我是否误解了虚拟化模式,或者我做错了什么?
I have the following ListBox:
<ScrollViewer>
<!--Spec Definitions-->
<ListBox DataContext="{Binding SpecPackageSpecGroupListViewModel}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True"
ItemContainerStyle="{StaticResource SpecPackageSpecGroupListBoxStyle}"
ItemsSource="{Binding SortedChildren}"
Background="Transparent"
BorderThickness="0" SelectionMode="Extended"
Margin="5,5,5,5">
<ListBox.ItemTemplate>
<DataTemplate>
<Controls:SpecPackageSpecGroupControl/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
This list box is supposed to host ~1000 items, but complex ones.
I want it to work with the VirtualizingStackPanel, so I have set the visualizing XAML configuration to:
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
My problem is that I think it doesn't work - first - it takes a very long time to load ~700 items, and secondly, when I hit a breakpoint on my control constructor - I can see it is called 700 times:
public static int Counter = 0;
public SpecPackageSpecGroupControl()
{
InitializeComponent();
Counter++;
if (Counter%100 == 0)
Console.WriteLine("Hi");
}
I break point on the Console.WriteLine("Hi") and I can see that the static counter reached 700.
So basically the UIElements are being created although this is a virtual mode.
Am I misunderstanding the virtualization mode, or is there something I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将其放在 ScrollViewer 中。您粘贴的 XAML 确实绕过了虚拟化,但出于不同的原因:ListBox 完全扩展(不滚动),因为scrollViewer 允许它这样做。因为它是完全“扩展”的,所以 ListBox 不使用虚拟化。如果将其放置在常规容器(边框、网格等)中,它将使用其内置的滚动查看器。
Don't put it in a ScrollViewer. The XAML as you pasted indeed bypasses virtualization but for a different reason: the ListBox extends fully (without scrolling) because the scrollViewer allows it to. Because it is fully 'extended', the ListBox doesn't use virtualization. It will use its built-in scroll viewer if you place it in a regular container - Border, Grid etc.