0

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?

G. B.
  • 528
  • 2
  • 15
  • 3
    How about DataTemplates in `Style.Resources`? – Clemens Nov 04 '19 at 16:10
  • that seems to be working, it also solves the problem of putting the templates into separate files. only question that remains, is how to put them into their own assembly. – G. B. Nov 04 '19 at 16:45
  • That doesn't make sense to me. You either have them in the Style in the control library, or somewhere in the application where you use the control. – Clemens Nov 04 '19 at 16:57
  • Makes sense, but what if I want to have a plug-in architecture? Does the ItemsControl have to know how to display the items, or should the items know, how they want to be displayed? – G. B. Nov 04 '19 at 17:06
  • What not use a DataTemplateSelector? – DerApe Sep 09 '20 at 09:19

0 Answers0