IsVirtualizingWhenGrouping and TreeView

本文关键字:TreeView and IsVirtualizingWhenGrouping | 更新日期: 2023-09-27 18:30:03

在对对象进行分组时,TreeView中的虚拟化存在问题。在.NET 4.5中添加了VirtualizingPanel.IsVirtualizingWhenGrouping这一有用的东西,我正试图在下一个代码中使用它:

<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>

当程序启动时,我展开包含子对象的父对象并滚动TreeView。当父对象移动到可见部分之外时,程序抛出NullReferenceException,Source:PresentationFrame,StackTrace:

在System.Windows.Controls.VirtualizingStackPanel.FindScrollOffset(Visualv)

在System.Windows.Controls.VirtualizingStackPanel.MakeVisible(Visual视觉,矩形)中的System.Windows.Controls.ScrollViewer.ExecuteNextCommand()System.Windows.Controls.ScrollViewer.OnLayoutUpdated(对象发送器,中的EventArgs e)中的System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()中的System.Windows.ContextLayoutManager.UpdateLayout()System.Windows.ContextLayoutManager.UpdateLayoutCallback(对象参数)
在System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()中
在System.Windows.MediaContext.RenderMessage HandlerCore(对象resizedCompositionTarget)System.Windows.MediaContext.RenderMessage处理程序(对象resizedCompositionTarget)System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托callback,Object args,Int32 numArgs)System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,委托回调,对象参数,Int32 numArgs,委托catchHandler)

此ecxeption在事件VirtualizingStackPanel.CleanUpVirtualizedItem.之前抛出

有什么办法解决它吗?谢谢你的回答!

UPD#1:在.Net 4.5.2和.Net 4.6.1平台

IsVirtualizingWhenGrouping and TreeView

上测试

我在4.6.1上遇到了同样的错误,我已经通过将解决方案升级到4.7.2.NET版本来修复它。