Whats the difference between ContentControl.Template and ContentControl.ContentTemplate

user966765 picture user966765 · Sep 27, 2011 · Viewed 17.7k times · Source

What's the difference between ContentControl.Template and ContentControl.ContentTemplate? And when do I use which?

For example I could write in a xaml file for WPF:

<ContentControl>
    <ContentControl.Template>
        <ControlTemplate>
            <Label Content="This is from the Template"/>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

Notice the ContentControl.Template Tag

or I could write this:

<ContentControl>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Label Content="This is From the ContentTemplate"/>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

Notice the ContentControl.ContentTemplate Tag

The output looks the same and in the first case I use a ControlTemplate and in the other a DataTemplate. However, how should I deceide if I have to use .Template or .ContentTemplate? And what implications does this have (e. g. on DataBinding, Property Inheritance, ...).

Answer

sll picture sll · Sep 27, 2011

Template property defines the appearence of a Control itself and ContentTemplate defines the template of the Content area of a Control. Interesting point from MSDN:

If a Control does not have a ControlTemplate, the Control will not appear in your application.

This becomes more clear when we take a look at the data types of both properties: