I am trying to create a ListBox with a decently reliable DataTemplate. The problem is that performance is incredibly slow. About 2.5 years on my home computer, it takes about 4-6 seconds to bind 80 lines. On my working computer (64 bit / two cores), the time is reduced to about 2.5 seconds, but still it seems very slow (and 150 lines take about 4-5 seconds).
All data is pure POCO with random data. There are absolutely no services in this application (yet). Random data generation does not require any time - I (roughly) do a MessageBox.Show after creating the data, before updating the ObservableCollection. (I even tried to bind to regular old lists - even slower). In addition, I tried it in both Chrome and IE.
Ok, here is the code, if I left something, let me know. As I said, this is a reliable DataTemplate, but I thought that was the meaning of SL / WPF - that they could do cool things like this. I hope that I am doing something wrong - this is the data that I receive from children.
EDIT - just to put the answer up, there is no reason why you ever told that the ListBox uses the StackPanel as the ItemsPanelTemplate element - it displays the elements stacked vertically by default, but not only indicates the StackPanel as superfluous, it also completely destroys performance. I uninstalled ItemsPanelTemplate and it worked beautifully.
<ListBox ItemsSource="{Binding Path=BookSource_oc}" ItemTemplate="{StaticResource BookDataTemplateMedium}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="2" HorizontalAlignment="Stretch" x:Name="stackPanelItemsPanel">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
from the XAML application:
<DataTemplate x:Key="BookDataTemplateMedium">
<Border CornerRadius="3" BorderThickness="2" BorderBrush="Black" Background="White">
<Grid Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel HorizontalAlignment="Left" Grid.Column="1" Margin="5,0,10,0">
<TextBlock FontWeight="Bold" TextTrimming="WordEllipsis" Foreground="Black" ToolTipService.ToolTip="{Binding CurrentBook.Title}" Text="{Binding CurrentBook.Title}"/>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="270"></ColumnDefinition>
<ColumnDefinition Width="270"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="150"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0">
<local:vwAmazonReviewsRightToLeft></local:vwAmazonReviewsRightToLeft>
<local:vwPublisherInfoWithDateLeftToRight Margin="0,5,0,0"></local:vwPublisherInfoWithDateLeftToRight>
<local:vwPagesInfo></local:vwPagesInfo>
<StackPanel Orientation="Horizontal">
<TextBlock>Is Read </TextBlock>
<Image Margin="8,0,0,0" Stretch="None" Source="{Binding IsReadImgUri, Mode=OneWay}"></Image>
</StackPanel>
<HyperlinkButton Margin="0,5,0,0" NavigateUri="http://www.google.com" Content="View at Amazon"/>
</StackPanel>
<ListBox Grid.Column="1" Margin="0,2,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" BorderThickness="0" ItemsSource="{Binding CurrentBook.Subjects}" ItemTemplate="{StaticResource dataTemplateSubjectsLB}" />
<ListBox Grid.Column="2" Margin="10,2,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top" BorderThickness="0" ItemsSource="{Binding CurrentBook.Authors}" ItemTemplate="{StaticResource dataTemplateAuthorsLB}" />
</Grid>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="dataTemplateSubjectsLB">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Image Source="Img/Bullets/Bullet_Purple.png"></Image>
<TextBlock ToolTipService.ToolTip="{Binding Name}" Text="{Binding Name}"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="dataTemplateAuthorsLB">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Image Source="Img/Bullets/Bullet_Red.png"/>
<TextBlock ToolTipService.ToolTip="{Binding Name}" Text="{Binding Name}"/>
</StackPanel>
</StackPanel>
</DataTemplate>