Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF:Difference between TabControl.ItemTemplate and TabItem.ContentTemplate

Tags:

c#

wpf

I'm confused on this for a long time,these both seem to affect the tabitems' presentation in the tabcontrol. Is it designed for best control of the presentation of the tabcontrol? Or if there is something I dont't understand.

like image 289
Andrew Carl Avatar asked Aug 12 '14 13:08

Andrew Carl


3 Answers

There's some very long answers here for what is actually a very simple question. To avoid confusion:

ItemTemplate is the template used to format each item in the ItemsSource to create the headers (the controls that appear in the tab bar) and ContentTemplate is the template used to format each item in the ItemsSource to create the content of the tabs (the controls that appear when you click on the header).

like image 175
Nathan Phillips Avatar answered Oct 15 '22 13:10

Nathan Phillips


The ItemsControl.ItemTemplate Property is used to define what each item in the data bound collection should look like... from the ItemsControl.ItemTemplate Property page on MSDN:

Gets or sets the DataTemplate used to display each item.

As you can see, it is of type DataTemplate, which is customary for a template that displays data... its DataContext will automatically be set to an item from the collection and so controls declared in that DataTemplate will automatically have access to the items properties. Please see the Data Templating Overview page on MSDN for further help with this.

Similarly, from MSDN, the ContentControl.ContentTemplate Property:

Gets or sets the data template used to display the content of the ContentControl.

Again, its DataContext will automatically be set to the object that is set as the Content property. Please note that the ContentControl only has a ContentTemplate Property and no ItemTemplate Property, which is used for collection items... from the Data Templating Overview page on MSDN:

Because myTaskTemplate is a resource, you can now use it on other controls that have a property that takes a DataTemplate type. As shown above, for ItemsControl objects, such as the ListBox, it is the ItemTemplate property. For ContentControl objects, it is the ContentTemplate property.


UPDATE >>>

To clarify this situation further, think of this simple rule:

Use the ContentTemplate property to define how an object that is set as the Content property of a ContentControl should look.

Use the ItemTemplate property to define how the items of a collection control should look.

That the difference at its simplest. However, I'd like to point out that as these properties are both of type DataTemplate, their values are interchangeable.

For example, let's say that you have a Person class and you display a collection of Person objects in a ListBox. You can declare a DataTemplate to set as the ListBox.ItemTemplate property to define how each Person in the collection should look. However, if you just wanted to display a single Person, then you could use a ContentControl with the Content set to an instance of the Person class, and still use the same DataTemplate, but set as the ContentTemplate instead:

Multiple objects:

<ListBox ItemsSource="{Binding People}" ItemTemplate="{StaticResource Template}" ... />

...

Single object:

<ContentControl Content="{Binding Person}" 
    ContentTemplate="{StaticResource Template}" ... />
like image 32
Sheridan Avatar answered Oct 15 '22 14:10

Sheridan


Setting the TabControl.ItemTemplate you specify a template to use for all TabItems in the Items collection of the TabControl, unless you override the TabItem.ContentTemplate for a specific TabItem.

So, while they do the same, TabControl.ItemTemplate is a more generic template for all the TabItems in the TabControl and TabItem.ContentTemplate is specific for the TabItem it is used in.

The above is not quite true, as TabControl has an ItemTemplate property and a ContentTemplate property, to make it more confusing.

ItemTemplate is used as template for the header (the tab thingy) of all TabItems added through databinding on the ItemsSource or through Xaml without making the the added item a TabItem:

<TabControl ItemsSource="{Binding ListOfItems}">
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red"/>
        </DataTemplate>
     </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding}" Foreground="Blue"/>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

This will create a TabControl with red text in the header/tab and blue text for content.

Now, if we do the following:

<TabControl>
    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red"/>
        </DataTemplate>
     </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
             <TextBlock Text="{Binding}" Foreground="Blue"/>
        </DataTemplate>
    </TabControl.ContentTemplate>

    <TabItem Header="One" Content="One"/>
    <TabItem Header="Two" Content="Two"/>
    <TabItem Header="Three" Content="Three"/>
</TabControl>

We'll have a TabControl with three tabs, and the header text is black, content is still blue. And a DataError informing us that the ItemTemplate and ItemTemplateSelector properties are ignored for items already of the ItemsControl's container type, in this case TabItem. In this case, we need to specify TabItem.HeaderTemplate to change the appearance of the header.

So TabControl.ItemTemplate and TabItem.ContentTemplate don't do the same, but my previous explanation still holds for TabControl.ContentTemplate and TabItem.ContentTemplate.

like image 45
Roel van Westerop Avatar answered Oct 15 '22 15:10

Roel van Westerop