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

发布于 2024-07-09 04:50:57 字数 1104 浏览 10 评论 0原文

我有一个 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>

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

所以我的问题是:

使用 ElementHost 是性能噩梦吗?

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

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

I have a Windows Forms app, that has a single ElementHost containing a WPF UserControl... in my WPF, I have a VERY simple 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).

So my question is:

Is using an ElementHost a performance nightmare?

Is WPF DataBinding a performance nightmare?

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

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

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

发布评论

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

评论(4

牵你手 2024-07-16 04:50:57

使用虚拟化

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

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> 
打小就很酷 2024-07-16 04:50:57

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

WPF:数据虚拟化
保罗·麦克莱恩
http://www.codeproject.com/KB/WPF/WpfDataVirtualization.aspx

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

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

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

披肩女神 2024-07-16 04:50:57

我遇到过一个案例,这里提供的答案并没有解决我的问题。 就我而言,将 ListViewMaxHeight 属性设置为大于实际显示高度的值立即解决了问题,这要归功于 这个答案在这里,即使我无法解释它是如何以及为什么起作用的。

I had a case where the answers presented here didn't solve my problem. In my case, setting the MaxHeight property of the ListView to 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.

ぃ弥猫深巷。 2024-07-16 04:50:57

我有同样的问题。 将列表视图替换为包含 DataGridTextColumn 项目的 DataGrid 解决了该问题。

<DataGrid Margin="4" ItemsSource="{Binding Notifications}">
    <DataGrid.Columns>
    <DataGridTextColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
    <DataGridTextColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
</DataGrid.Columns>

I had the same problem. Replacing the listview with DataGrid containing DataGridTextColumn items fixed the problem.

<DataGrid Margin="4" ItemsSource="{Binding Notifications}">
    <DataGrid.Columns>
    <DataGridTextColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
    <DataGridTextColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
</DataGrid.Columns>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文