I need a single scrollable surface that contains two bound lists. At first, I used a ScrollViewer with two ListBox inside, each having their scrolling disabled, so I could still have item selection. Seeing the poor loading time performance, I changed my ListBoxes to ItemsControl, but the performance is still terrible. In total, my two lists have only 110 items.
<ScrollViewer Grid.Row="1">
<StackPanel>
<Button Style="{StaticResource EmptyNonSelectButtonStyle}" BorderThickness="0" HorizontalContentAlignment="Left" Click="AnyCityButton_Click">
<TextBlock Text="{Binding Resources.CurrentLocationItem, Source={StaticResource LocalizedResources}}" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeLarge}" />
</Button>
<TextBlock Text="{Binding Resources.TopTenCitiesHeader, Source={StaticResource LocalizedResources}}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="12,12,12,8" />
<ItemsControl ItemsSource="{Binding TopTenCities}" ItemTemplate="{StaticResource CityDataTemplate}" HorizontalContentAlignment="Stretch" />
<TextBlock Text="{Binding Resources.TopHundredCitiesHeader, Source={StaticResource LocalizedResources}}" Style="{StaticResource PhoneTextSubtleStyle}" Margin="12,12,12,8" />
<ItemsControl ItemsSource="{Binding TopHundredCities}" ItemTemplate="{StaticResource CityDataTemplate}" HorizontalContentAlignment="Stretch" />
</StackPanel>
</ScrollViewer>
What can I do to improve performance? I’ve tried setting the ItemsSource after the page loading, but it still ugly (empty lists for a few seconds), doesn’t make more sense.
Thank you.
This answer has turned into a monster but slog through it and I think you’ll find an answer.
We need in some way to use the
VirtualizingStackPanelasListBox. We need to collect all the items to display (the button, the two textblocks and two sets of city data) into a single enumerable of some type. The the real trick and would be to determine one of three templates to use to render the items.Bottom line is we need to create a new type of
ItemsControl. Now we can gain a little advantage by simply accepting we want to create a very specificItemsControlthat supports only this task. First here is a “starter for 10” (a UK media reference).A really dumb example of creating a specific items control:-
This control assumes its items are a set of integers. It has an
AlternativeItemTemplatewhich if supplied it toggles between on an odd/even basis (note that is a facet of the item).Now lets put that use with a
VirtualizingStackPanel:-Note the
ItemsPanelis using theVirtualizingStackPaneland that gets presented in aScrollViewer.Now we can give it lot of content:-
}
If you switch to a standard
StackPanelthis takes ages to load, whereas it appears instant with virtualizing.Armed with this info you should be able to create a special ItemsControl which has the properties:-
IEnumerable<City>)IEnumerable<City>)Now you can create a single enumerable with some Linq extension methods:-
Now you just need to override
PrepareContainerForItemOverrideand choose betweenButtonTemplate(for the first null item), theHeaderTemplatefor item of type string or theItemTemplatefor an item of typeCity.