nuclear sweet
nuclear sweet

Reputation: 1121

Select DataTemplate for ItemsControl depending on ViewModel type

I tried to make my ItemsControl display items with different templates depending on its viewModel type. At first look i was think its possible to do like we usually do in ContentControl:

 <ItemsControl ItemsSource="{Binding MyViewModelCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type myNameSpace:myViewModel1}">
                     <myNameSpace:myControl2/>
                </DataTemplate>
                <DataTemplate DataType="{x:Type myNameSpace:myViewModel2}">
                     <myNameSpace:myControl2/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

But unfortunately you can not have two DataTemplate in ItemsControl.ItemTemplate. I found a little trick to do that.

Upvotes: 2

Views: 2837

Answers (2)

Fratyx
Fratyx

Reputation: 5797

In your XAML code just replace the ItemsControl.ItemTemplate tag by ItemsControl.Resources and it should work.

<ItemsControl ItemsSource="{Binding MyViewModelCollection}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type myNameSpace:myViewModel1}">
             <myNameSpace:myControl2/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type myNameSpace:myViewModel2}">
             <myNameSpace:myControl2/>
        </DataTemplate>
    </ItemsControl.Resources>
</ItemsControl>

Upvotes: 8

nuclear sweet
nuclear sweet

Reputation: 1121

So you can put ContentControl into ItemsControl DataTemplate and it works.

        <ItemsControl ItemsSource="{Binding MyViewModelCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding}">
                        <ContentControl.Resources>
                           <ResourceDictionary>
                             <DataTemplate DataType="{x:Type myNameSpace:myViewModel1}">
                                <myNameSpace:myControl2/>
                             </DataTemplate>
                             <DataTemplate DataType="{x:Type myNameSpace:myViewModel2}">
                                <myNameSpace:myControl2/>
                             </DataTemplate>
                          </ResourceDictionary>
                       </ContentControl.Resources>
                    </ContentControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

Upvotes: 0

Related Questions