2

I have a WPF app that I've created using basic MVVM principles.

In one of my Views, I have a ListBox control. It is bound to an ObservableCollection on my ViewModel.

Roughly, the layout is like this:

<UserControl>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ListBox ItemsSource="{Binding Students}" 
                 ItemTemplate="{StaticResource Template_Student}"  />
        <ListBox ItemsSource="{Binding Logs}" />
    </Grid>
</UserControl>

<DataTemplate x:Key="Template_Student">
    <Grid Width="300" Height="85">
        <Grid.RowDefinitions>

        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Image Grid.RowSpan="3" Margin="5 10"
               Height="65" Width="50"
               Source="{Binding Property7}">
        </Image>
        <StackPanel Grid.Column="1" Margin="2 10 2 2">
            <TextBlock>
                <Run Text="{Binding Property4, StringFormat=\{0\}\,}" FontWeight="Bold" FontSize="14" />
                <Run Text="{Binding Property5}" FontWeight="Bold" FontSize="14" />
                <Run Text="{Binding Property6, StringFormat=(\{0\})}" />
            </TextBlock>
            <TextBlock>
                <Run Text="Property1:" />
                <Run Text="{Binding Property1}" />
            </TextBlock>
            <TextBlock>
                <Run Text="Property2:" />
                <Run Text="{Binding Property2}" />
            </TextBlock>
            <TextBlock>
                <Run Text="Property3:" />
                <Run Text="{Binding Property3, Mode=OneWay}" />
            </TextBlock>
        </StackPanel>
    </Grid>
</DataTemplate>

The ListBox uses the default VirtualizingStackPanel and I confirmed, via Snoop, that it is using it and that VirtualizingStackPanel.IsVirtualizing is true.

The Students property of the ViewModel has about 1000 objects in it. They're not that heavy. About 11 INPC-enabled properties.

In this scenario, the Window redrawing for the entire window lags considerably.

Simply restoring the size of the window or maximizing it causes a delay of more than second. Adding something to the Logs property (also an ObservableCollection of very lightweight objects) that causes a delay of more than a second while the window redraws.

The delay appears to have some correlation with two things:

  1. The number of items in the ListBox. If I cut it down to 10 items, the lag is completely gone. If I do 250 items, it is noticeable but not terrible. 500 starts to get bad and 1000, the number in the actual dataset, is unusable.
  2. The complexity of the DataTemplate. If I remove both the Image and the StackPanel with all of the properties, the performance is better (akin to about 100 items with all the data). If I leave in either one, the performance is bad again.

I'm wondering if this has something to do with the way I've chosen to lay things out (all of the Grid ColumnDefinitions having a Width of either Auto or *, for instance). Or if maybe I'm just doing something completely wrong that is undermining the Virtualization.

Any help would be appreciated.

1 Answer 1

0

I had almost the exact same scenario. Turns out WPF ListBox performs really poorly for >100 items, even with no custom templates.

I don't know of any open source or free alternatives, but we tried Xceed Extended WPF Toolkit Plus (commercial*) and its drop-in replacement Xceed Ultimate ListBox for WPF.

It has a bunch of optimizations and the performance improvement for us was huge. With around 500 items in the list and custom templates, resizing the windows used to be ridiculously slow. Now it's quite smooth.

* I am in no way affiliated with Xceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.