Reputation: 1063
I have a TreeView with 2 level Hierarchy. TreeView Item has got biding to the property of particular class. Basically I am getting the ObservableCollection list and assiging that list as my TreeView ItemsSource. Initially treeView visibility set to Collapsed and after assigning the ItemsSource, TreeView visibility set to Visible. At this point app freezes for about 40 secs (Its basically loading 1000s of parent item and 5-10 children for each parent).
Code behinnd is
PopulateTreeView()
{
// ocListToDisplay is my ObservableCollection
tvES.ItemsSource = ocListToDisplay;
//App Freezes at this bit when making the tvES Visible from Collapsed
tvES.Visibility = System.Windows.Visibility.Visible;
}
My XAML is like this
<ScrollViewer Name="scContainer" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<StackPanel>
<StackPanel.Resources>
<exportImport:TreeViewDashboard x:Key="dataItems"/>
<HierarchicalDataTemplate DataType="{x:Type exportImport:TreeViewDashboard}"
ItemsSource="{Binding Path=Components}">
<Grid Margin="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Name="checkbx" Grid.Column="0" Margin="2"
Tag="{Binding Path=Tag}"
Checked="OnCheck" Unchecked="OnUnCheck"></CheckBox>
<Image Margin="2" Grid.Column="1" Source="{Binding Path=ImageUrl}"
Height="14" Width="16" ></Image>
<TextBlock Margin="2" Grid.Column="2" Text="{Binding Path=Name}"
FontWeight="Bold" />
</Grid>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Grid Margin="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Name="checkbx" Grid.Column="0" Margin="2"
Tag="{Binding Path=Tag}"
Checked="OnCheck" Unchecked="OnUnCheck"/>
<Image Margin="2" Grid.Column="1"
Source="{Binding Path=ImageUrl}"
Height="14" Width="16" />
<TextBlock Margin="2" Grid.Column="2"
Text="{Binding Path=Name}" FontWeight="Bold" />
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</StackPanel.Resources>
<--same result if you set IsVirtualizing to True/False App freezes for 40 sec-->
<TreeView Name="tvES" BorderThickness="0"
ItemsSource="{Binding Source={StaticResource dataItems}}"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Recycling"
TreeViewItem.Expanded="item_Expanded"
TreeViewItem.Collapsed="item_Collapsed" >
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="False" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</StackPanel>
</ScrollViewer>
I also tried to get the number of visuals in my tree view. I get the same number of Visuals count in both the cases of IsVisualizing="True" and IsVisualizing="False" That clearly indicating that I am doing something wrong.
Code to get visual count from the tree
//In my case I am passing tvES as DependencyObject
private static int GetVisualCount(DependencyObject visual)
{
int visualCount = 1;
int childCount = VisualTreeHelper.GetChildrenCount(visual);
for (int i = 0; i < childCount; i++)
{
DependencyObject childVisual = VisualTreeHelper.GetChild(visual, i);
visualCount += GetVisualCount(childVisual);
}
return visualCount;
}
I get the treeview eventually but the problem is my app freezes while populating the treeview..!! Anyone got the workaound for this?? I have got scollviewer, will that causing problem to virtualization?
PS: TreeView Generated is like this but have 1000's of similar nodes
Updates:
I was reading about VirtualizingStackPanel where it says - "Virtualization in a StackPanel only occurs when the items control contained in the panel creates its own item containers. You can ensure this happens by using data binding. In scenarios where item containers are created and added to the items control, a VirtualizingStackPanel offers no performance advantage over a StackPanel."
can someone suggest me if the above approach which i am following is right?? I am pretty much sure that I am I am on wrong side..
Upvotes: 3
Views: 2486
Reputation: 10865
You forgot to set ScrollViewer.CanContentScroll=True
. That is a key
for virtualization. And put the IsVirtualizing
back to true
Upvotes: 2