Regarding performance the worst thing you can do is create new instances of a UserControl
over and over again. So basically what your solution (i) is suggesting.
Why? Because the xaml
of a UserControl
is always parsed for every instance you are creating.
So what can be done? You could design your control as a templated control
. This way the template xaml is only parsed once, and reused whenever you instantiate an new instance.
But it would still be inadvisable to create new instances in your code. Silverlight has item controls
that are able to handle large lists of entries and that can manage to only instantiate visual elements for entries that are actually visible.
So I strongly recommend using an ItemsControl
with a virtualizing panel (only instantiating visual elements for visible entries).
<ItemsControl>
<ItemsControl.ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsControl.ItemsPanelTemplate>
<ItemsControl.ItemTemplate>
<DataTemplate>
<MyItemControl/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Some code for MyItemControl:
public class MyItemControl : Control
{
public MyItemControl(){DefaultStyleKey=typeof(MyItemControl);}
}
Some xaml for the control's default template:
<Style TargetType="MyItemControl">
<Setter Property="Template" Value="{StaticResource MyItemControlDefaultTemplate}"/>
</Style>
<ControlTemplate x:Key="MyItemControlDefaultTemplate" TargetType="MyItemControl">
...
</ControlTemplate>