expanded issue in virtualized treeview
나래 정
1
Reputation point
I decided to use UI virtualization, because there are a lot of items in the treeview.
But there are some problems.
- When I expand a node with 1000 child nodes, the UI does not display normally.
- And it has scrolling issues: not smooth scrolling, and sometimes program abnormal terminated.
xaml
<TreeView Grid.Column="2" Grid.Row="2" Visibility="Visible" ItemsSource="{Binding Children}" VirtualizingStackPanel.IsVirtualizing="True" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type vm:TreeItemViewModel}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Margin="2">
<Border Background="YellowGreen" CornerRadius="3" Width="16" Height="16" SnapsToDevicePixels="True" />
<TextBlock Text="{Binding DisplayName}" VerticalAlignment="Center" Padding="4,0,2,0"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type vm:ColorItemViewModel}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="Colour:" Margin="1,0,0,0"/>
<TextBlock
Text="{Binding DisplayName}"
Background="{Binding BackgroundBrush}"
Foreground="{Binding ForegroundBrush}"
Padding="3,0"
Margin="6,0,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="IsEnabled" Value="{Binding IsEnabled, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
cs
/// <summary>
/// Gets/sets whether the TreeViewItem
/// associated with this object is expanded.
/// </summary>
public bool IsExpanded
{
get { return isExpanded; }
set
{
if (value != isExpanded)
{
isExpanded = value;
OnPropertyChanged("IsExpanded");
// Expand all the way up to the root.
if (isExpanded && parent != null)
parent.IsExpanded = true;
// Lazy load the child items, if necessary.
if (isExpanded && HasDummyChild)
{
Children.Remove(DummyChild);
LoadChildren();
}
}
}
}
/// <summary>
/// Invoked when the child items need to be loaded on demand.
/// Subclasses can override this to populate the Children collection.
/// </summary>
protected virtual void LoadChildren()
{
for (int i = 0; i < 5000; i++)
{
Children.Add(new TreeItemViewModel(this, true) { DisplayName = "subnode " + i });
}
}
When a node is expanded, 1000 sub-nodes are added(Lazy load).
How can I solve these problems?
Sign in to answer