我正在寻找一种方法,以强制TreeView在使用Binding时(通过展开分支)在实际显示所有子级(TreeViewItem)之前创建其所有子级。
我有一棵树,有多个等级,在某些等级中,有很多孩子(超过200个)。当扩展带有许多子项的分支时,需要花费几秒钟来创建和渲染子项。 我知道,可以使用虚拟化来避免此问题,并且确实可以。但是,它还引入了其他一些问题,包括滚动时的滞后(我使用的Material Design看起来确实不错,但是会带来很多开销)。我可以将所有分层项的IsExpaned设置为true,因此可以强制立即渲染所有TreeViewItems,但是我不想从一开始就将它们全部展开。
这是我正在使用的TreeView的最低版本。 TreeViewList是一个带有子级和字符串“名称”的ObservableCollection。
<TreeView Name="treeview"
Style="{StaticResource ProjectViewTreeStyle}"
FontSize="14"
MinWidth="200"
ItemsSource="{Binding TreeViewList, UpdateSourceTrigger=PropertyChanged}"
SelectedItemChanged="TvMain_SelectedItemChanged">
<TreeView.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
Color="{StaticResource {x:Static SystemColors.HighlightColorKey}}" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="{StaticResource {x:Static SystemColors.HighlightTextColorKey}}"/>
<HierarchicalDataTemplate DataType="{x:Type classes:TreeViewElementTreeNode}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="Folder" Foreground="{StaticResource TreeViewFolderForeground}"/>
<TextBlock Text="{Binding Path=Name}" Margin="5,0,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type classes:TreeViewElementItemNode}">
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="FileDocumentBoxOutline" />
<TextBlock Text="{Binding Path=Name}" Margin="5,0,0,0"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
在样式内,我绑定了IsExpanded和IsSelected。
我的问题是,是否有可能在不扩展所有分支的情况下强制创建和渲染所有TreeViewItem,因为在我的用例中,第一次绑定数据时可以有更大的延迟,但在操作过程中则没有延迟。 内存不是问题,因此不需要虚拟化,也不希望如上所述。