I have code as this one:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyViewModel}">
<!-- xaml is typed here directly -->
<Border>
...
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And xaml inside the DataTemplate is big (more than 200 lines).
I want to move xaml which is inside the DataTemplate into a separate UserControl to make it easier to edit and maintain. I do next:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyViewModel}">
<!-- xaml is moved to separate UserControl -->
<local:MyViewModelUserControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The issue I run into is that rendering/processing the second code (with UserControl) takes about 2 times longer than the 1st code. Any ideas how to deal with it?
NOTE: I'm moving not the ListBox, but the xaml which is inside the DataTemplate. The reason is not to reuse this code, but to minimize the main file where the ListBox is placed. Other thing is that I have several DataTemplates inside the ListBox (for several ViewModels) and the xaml is really huge. That's why I want to move this xaml (which is inside the DataTemplate) to a separate control.