WPF ListView 性能非常慢 - 为什么?(ElementHost,还是其他原因?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:48:21  来源:igfitidea点击:

WPF ListView Very Slow Performance - Why? (ElementHost, or Other Reason?)

wpfperformancelistviewdatagridelementhost

提问by Timothy Khouri

I have a Windows Forms app, that has a single ElementHost containing a WPF UserControl... in my WPF, I have a VERY simple ListView:

我有一个 Windows 窗体应用程序,它有一个包含 WPF UserControl 的 ElementHost ......在我的 WPF 中,我有一个非常简单的 ListView:

<ListView Margin="4" ItemsSource="{Binding Notifications}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
            <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
            <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}" />
            <GridViewColumn Header="City" DisplayMemberBinding="{Binding City}" />
            <GridViewColumn Header="State" DisplayMemberBinding="{Binding State}" />
            <GridViewColumn Header="Zip" DisplayMemberBinding="{Binding Zip}" />
        </GridView>
    </ListView.View>
</ListView>

If my source has 10 items, the form loads in less than one second. If my source has 1000 items, it takes 7 seconds!!! My timer is ONLY taking the loading into account (not how much time it takes to get the items).

如果我的源有 10 个项目,表单会在不到一秒的时间内加载。如果我的源有 1000 个项目,则需要 7 秒!!!我的计时器只考虑加载(而不是获取物品所需的时间)。

So my question is:

所以我的问题是:

Is using an ElementHost a performance nightmare?

使用 ElementHost 是性能噩梦吗?

Is WPF DataBinding a performance nightmare?

WPF 数据绑定是性能噩梦吗?

Is the ListView a piece of crap? (btw, same results with the WPFToolkit's DataGrid)?

ListView 是废话吗?(顺便说一句,与 WPFToolkit 的 DataGrid 的结果相同)?

回答by

Use virtualization

使用虚拟化

<ListView ItemsSource="{BindingNames}"Name="lv">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                   <!--<StackPanel/>
                    If StackPanel was used, the memory consumed was over 2GB and dead slow.
                    -->
                   <VirtualizingStackPanel>
                    <!--Memory footprint is only 200 mb-->
                    </VirtualizingStackPanel>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView> 

回答by Tawani

You may also want to check this excellent article on the Code Project:

您可能还想查看这篇关于代码项目的优秀文章:

WPF: Data Virtualization By Paul McClean http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

WPF:数据虚拟化 作者:Paul McClean http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

It show you a much better approach at minimal memory and bandwidth usage.

它以最少的内存和带宽使用量向您展示了一种更好的方法。

回答by Simon Voggeneder

I had a case where the answers presented here didn't solve my problem. In my case, setting the MaxHeightproperty of the ListViewto a value larger than the actual displayed height solved it immediately, thanks to this answer here, even if I cannot explain how and why it worked.

我有一个案例,这里提供的答案没有解决我的问题。就我而言,将 的MaxHeight属性设置为ListView大于实际显示高度的值立即解决了这个问题,多亏了这里的这个答案,即使我无法解释它是如何以及为什么工作的。