I need to visualize a list of UserControls with a DataGrid within a ScrollViewer in the MainWindow but it seems that the rendering of the scrollable list takes a long time and I don't know where I am doing wrong.
This is the CollectionUserControl.xaml with the DataGrid
<DataGrid
EnableColumnVirtualization="True"
EnableRowVirtualization="True"
Grid.Row="1"
Visibility="{Binding CurrentCollection.CollectionVisibility}"
Background="Transparent"
VerticalAlignment="Center"
VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingStackPanel.IsVirtualizing="True"
VerticalContentAlignment="Center"
Margin="0,0,0,0"
ItemsSource="{Binding CurrentCollection.Properties}"
CanUserAddRows="False"
CanUserDeleteRows="False"
HeadersVisibility="Column"
RowHeight="40"
CanUserResizeColumns="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
...list of columns..
</DataGrid.Columns>
</DataGrid>
In the MainView I used a DataTemplate to set the CollectionUserControl.xaml as the template for the Collection_ViewModel.cs. Then I added a scrollviewer with an ItemsControl that binds the list of the ViewModels.
<Window.Resources>
<DataTemplate
DataType="{x:Type viewModels:Collection_ViewModel}">
<local:CollectionUserControl />
</DataTemplate>
</Window.Resources>
<ScrollViewer
Grid.Row="3"
VerticalScrollBarVisibility="Auto"
CanContentScroll="True"
HorizontalScrollBarVisibility="Disabled"
Grid.ColumnSpan="2"
PreviewMouseWheel="ScrollViewer_PreviewMouseWheel">
<ItemsControl
Grid.Row="3"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="True"
ItemsSource="{Binding SelectedConfiguration.CollectionViewModels}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
I tried all the things suggested in this post (enable virtualization of rows and columns, virtualization mode recycling, virtualizing stack panel,...) but it all seems useless, the scrollviewer and the list of UserControls takes up to 10 seconds to render which is a very long time considering that I have just 15 UserControls to load.
What can I do to improve performances?