Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
This isn't the cleanest approach but at least it works. One can define two different templates (each of them contains ItemsControl with single difference in ItemsPanelTemplate) and switch between ...
Answer
#1: Initial revision
This isn't the cleanest approach but at least it works. One can define two different templates (each of them contains `ItemsControl` with single difference in `ItemsPanelTemplate`) and switch between them as needed with a simple "selector": C#: ```csharp public class ViewTypeTemplateSelector : IDataTemplate { [Content] public Dictionary<ViewType, IDataTemplate> Templates { get; } = new Dictionary<ViewType, IDataTemplate>(); public IControl Build(object data) => Templates[(ViewType)data].Build(data); public bool Match(object data) => data is ViewType; } ``` xaml: ```xaml <ContentControl Grid.Row="0" Content="{Binding ViewType}"> <ContentControl.Resources> <DataTemplate x:Key="itemTemplate"> <TextBlock Text="{Binding}" Margin="2" Padding="2"/> </DataTemplate> </ContentControl.Resources> <ContentControl.DataTemplates> <views:ViewTypeTemplateSelector> <DataTemplate x:Key="H"> <ItemsControl ItemTemplate="{StaticResource ResourceKey=itemTemplate}" Items="{Binding Values}" DataContext="{Binding $parent[Window].DataContext}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </DataTemplate> <DataTemplate x:Key="V"> <ItemsControl ItemTemplate="{StaticResource ResourceKey=itemTemplate}" Items="{Binding Values}" DataContext="{Binding $parent[Window].DataContext}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> </DataTemplate> </views:ViewTypeTemplateSelector> </ContentControl.DataTemplates> </ContentControl> ```