ListView非常慢的性能

ListView非常慢的性能

本文介绍了WPF ListView非常慢的性能 - 为什么? (ElementHost或其他原因?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < ListView Margin =4ItemsSource ={Binding Notifications}> 
< ListView.View>
< GridView>
< GridViewColumn Header =FirstNameDisplayMemberBinding ={Binding FirstName}/>
< GridViewColumn Header =LastNameDisplayMemberBinding ={Binding LastName}/>
< GridViewColumn Header =AddressDisplayMemberBinding ={Binding Address}/>
< GridViewColumn Header =CityDisplayMemberBinding ={Binding City}/>
< GridViewColumn Header =StateDisplayMemberBinding ={Binding State}/>
< GridViewColumn Header =ZipDisplayMemberBinding ={Binding Zip}/>
< / GridView>
< /ListView.View>
< / ListView>

如果我的源有10个项目,则表单加载不到一秒钟。如果我的来源有1000个项目,需要7秒!我的计时器只考虑加载(不需要多少时间获取项目)。



所以我的问题是:



是否使用ElementHost表演噩梦?



WPF DataBinding是否表现噩梦?



ListView是一个垃圾? (btw,与WPFToolkit的DataGrid相同的结果)?

解决方案

使用虚拟化

 < ListView ItemsSource ={BindingNames}Name =lv> 
< ListView.ItemsPanel>
< ItemsPanelTemplate>
<! - < StackPanel />
如果使用StackPanel,则消耗的内存超过2GB,死缓。
- >
< VirtualizingStackPanel>
<! - 内存占用只有200 mb - >
< / VirtualizingStackPanel>
< / ItemsPanelTemplate>
< /ListView.ItemsPanel>
< ListView.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding}/>
< / DataTemplate>
< /ListView.ItemTemplate>
< / ListView>


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

解决方案

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>

这篇关于WPF ListView非常慢的性能 - 为什么? (ElementHost或其他原因?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:08