VirtualizingStackPanel 不工作

发布于 2024-09-18 23:28:53 字数 1474 浏览 9 评论 0原文

我有以下列表框:

<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 技术交流群。

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

发布评论

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

评论(1

作死小能手 2024-09-25 23:28:53

不要将其放在 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.

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