I thought I would talk about the details of a solution, which is pretty much a direct implementation of Sinatr's answer.
I used a content control with a very simple data template selector. The template selector simply checks to see if the content item is null and selects between two data templates:
public class VirtualizationNullTemplateSelector : DataTemplateSelector { public DataTemplate NullTemplate { get; set; } public DataTemplate Template { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { if (item == null) { return NullTemplate; } else { return Template; } } }
The reason for this is that the ContentControl that I used still exposes the data pattern, even if the content is null. So I installed these two patterns in xaml:
<ContentControl Content="{Binding VirtualizedViewModel}" Grid.Row="1" Grid.ColumnSpan="2" > <ContentControl.Resources> <DataTemplate x:Key="Template"> <StackPanel> ...complex layout that isn't often seen... </StackPanel> </DataTemplate> <DataTemplate x:Key="NullTemplate"/> </ContentControl.Resources> <ContentControl.ContentTemplateSelector> <Helpers:VirtualizationNullTemplateSelector Template="{StaticResource Template}" NullTemplate="{StaticResource NullTemplate}"/> </ContentControl.ContentTemplateSelector> </ContentControl>
Finally, instead of using a whole new class for the subitem, it’s quite simple to create a “VirtualizedViewModel” object in your view model that references “this”:
private bool expanded; public bool Expanded { get { return expanded; } set { if (expanded != value) { expanded = value; NotifyOfPropertyChange(() => VirtualizedViewModel); NotifyOfPropertyChange(() => Expanded); } } } public MyViewModel VirtualizedViewModel { get { if (Expanded) { return this; } else { return null; } } }
I have reduced my 2-3s boot time by about 75%, and now that seems a lot more reasonable.
source share