I have a custom control that subclasses ItemsControl
DesignerSheetView.cs:
public class DesignerSheetView : ItemsControl
{
static DesignerSheetView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DesignerSheetView), new FrameworkPropertyMetadata(typeof(DesignerSheetView)));
}
}
And the default style is set in DesignerSheetView.xaml:
<Style TargetType="{x:Type sheets:DesignerSheetView}" BasedOn="{StaticResource {x:Type ItemsControl}}">
<Setter Property="ItemsControl.ItemTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type items:LineItemViewModel}">
<Line X1="0" Y1="0" X2="{Binding X2}" Y2="{Binding Y2}" Stroke="{Binding Stroke}" StrokeThickness="{Binding Thickness}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsControl.ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsControl.ItemContainerStyle">
<Setter.Value>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Top" Value="{Binding Path=YPos}" />
<Setter Property="Canvas.Left" Value="{Binding Path=XPos}" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</Setter.Value>
</Setter>
</Style>
The instantiation looks like this in MainWindow.xaml:
<sheets:DesignerSheetView Background="Beige" ItemsSource="{Binding SheetElements}" >
</sheets:DesignerSheetView>
My problem is, that I want to display multiple item types, not just LineItemViewModel, but for e.g. RectangleItemViewModel or EllipseItemViewModel. I know, that normally I could do this, by setting the ItemsControl.Resources tag. I found this, this and this questions, but they are using UserControls (?), not a CustomControl with default style.
How do I add multiple ItemTemplates in this case? I guess one option would be to set the resources in MainWindow.xaml at instantiation, like this:
<sheets:DesignerSheetView Background="Beige" ItemsSource="{Binding SheetElements}" >
<ItemsControl.ItemTemplate>
<DataTemplate DataType="...">
</DataTemplate>
<DataTemplate DataType="...">
</DataTemplate>
</ItemsControl.ItemTemplate>
</sheets:DesignerSheetView>
But I'd like to avoid this, and keep the control boxed somehow, to keep all the templates, styles, etc. together in the DesignerSheetView folder, if possible in separate files.
Even better would be to keep the ItemTemplates of the xxxItemViewModels in their separate assembly. Is it possible? How do I do that?