I have a DataGrid with VirtualizationMode="Recycling" that is bound to an ObservableCollection in my ViewModel. It works fine – scrolling and page up/down is fast.
Now I add a further DataGridTemplateColumn with an ItemsControl in its DataTemplate. It is bound to about 15 items (for all rows same count). Now scrolling is much much slower. But if the underlying Collection is Null all is fast again. I assume that the generated ItemContainers in each row will not be recycled.
Is there a way to speed up the whole process or even better recycle all nested controls in the row?
PS: I would prefer a ItemsControl in a DataGridColumn, not a DataGridColumn for each Item in Collection.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding AObservableCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding AStringProperty}" Width="40" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
After doing some further research I can say: the problem is not the creation of container itself, but the content of container. 15 TextBoxes are heavier then I thought. I’ve replaced the
TextBoxwithTextBlockand it was as fast as before.Because I need editing I changed
DataGridTemplateColumn.CellTemplateintoDataGridTemplateColumn.CellEditingTemplateand created a new CellTemplate with a TextBlock. I switch the templates with:so only the selected cell/row shows a
TextBox.Of course, now I have new problems with Focusmanagement and Keyboardnavigation. But I think I have to do it this way.