views:

4327

answers:

2

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

+7  A: 

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>
That's both amazing and astonishing. Is there anyway to get the same look/feel of using GridView instead of DataTemplating things myself?
Timothy Khouri
Yes. Just put the "<ListView.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </ListView.ItemsPanel>" part in your code. This is the part which speeds up things.
Note that VirtualizingStackPanels are the default items panel template for ListViews. Using some features like grouping will override the default, however.
Scott Bilas
+1  A: 

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.

Tawani