views:

379

answers:

1

I have a WPF TabControl that I have set the ItemTemplate as well as the ContentTemplate. This tab control displays Call Log information based on customer tech support information.

Inside of this same control, I would also like to be able to show a ReturnAuthorization template.

I would like to swap these out based on the object type added to the TabControl's Items collection. Is this something that is possible?

I have some Pseudo-code that sort of shows what I want to pull off:

<TabControl x:Name="tabCases" IsSynchronizedWithCurrentItem="True" ItemContainerStyle="{StaticResource ClosableTabItemTemplate}"  >
        <TabControl.ItemTemplate>
            if ( Type is Entities:Case )
            {
            <DataTemplate DataType="{x:Type Entities:Case}">
                    <TextBlock Text="{Binding Path=Id}" />
            </DataTemplate>
            }
            else if ( Type is Entities1:RAMaster )
            {
            <DataTemplate DataType="{x:Type Entities1:RAMaster}">
                <TextBlock Text="{Binding Path=Id}" />
                </DataTemplate>
            }
        </TabControl.ItemTemplate>
        <TabControl.ContentTemplate>
            <DataTemplate DataType="{x:Type Entities:Case}">
                <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" />
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>
A: 

One way to do this is using something like an ItemTemplateSelector, which you can set on the TabControl. However, if you only need the different templates within the TabControl and know what they all are ahead of time, then you can just let them automatically be applied by using the DataTypes.

<TabControl x:Name="tabCases" IsSynchronizedWithCurrentItem="True" ItemContainerStyle="{StaticResource ClosableTabItemTemplate}"  >
    <TabControl.Resources>

        <DataTemplate DataType="{x:Type Entities:Case}">
                <TextBlock Text="{Binding Path=Id}" />
        </DataTemplate>

        <DataTemplate DataType="{x:Type Entities1:RAMaster}">
            <TextBlock Text="{Binding Path=Id}" />
        </DataTemplate>

    </TabControl.Resources>
    <TabControl.ContentTemplate>
        <DataTemplate DataType="{x:Type Entities:Case}">
            <CallLog:CaseReadOnlyDisplay DataContext="{Binding}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>
rmoore