I have a problem with virtualization in TreeView when grouping objects. In .NET 4.5 was added helpful thing as VirtualizingPanel.IsVirtualizingWhenGrouping, and I am trying to use it in next code:
<UserControl><UserControl.Resources>
<CollectionViewSource Source="{Binding SharedDevices}" x:Key="Cvs">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Assignment" />
<PropertyGroupDescription PropertyName="Location" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<TreeView ItemsSource="{Binding Source={StaticResource Cvs}}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingStackPanel.VirtualizationMode="Standard"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.ScrollUnit='Item'
VirtualizingPanel.CacheLengthUnit="Item"
>
<TreeView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Path=Name}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</TreeView.GroupStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="data:Device"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
When program launched, I expand parent object with included children and scroll TreeView. When parent object moved beyond the visible part, the program throws NullReferenceException, Source: PresentationFramework, StackTrace:
in System.Windows.Controls.VirtualizingStackPanel.FindScrollOffset(Visual v)
in System.Windows.Controls.VirtualizingStackPanel.MakeVisible(Visual visual, Rect rectangle) in System.Windows.Controls.ScrollViewer.ExecuteNextCommand() in System.Windows.Controls.ScrollViewer.OnLayoutUpdated(Object sender, EventArgs e) in System.Windows.ContextLayoutManager.fireLayoutUpdateEvent() in System.Windows.ContextLayoutManager.UpdateLayout() in System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
in System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
in System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) in System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) in System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
This ecxeption throws before event VirtualizingStackPanel.CleanUpVirtualizedItem.
Is there any way to fix it? Thank You for answers!
UPD#1: tested on .Net 4.5.2 and .net 4.6.1 platforms