Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between ContentControl.Template and ContentControl.ContentTemplate

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, ...).

like image 696
user966765 Avatar asked Sep 27 '11 10:09

user966765


People also ask

What is difference between control template and DataTemplate?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).

What's the difference between ContentControl and ContentPresenter in WPF?

ContentControl is a base class for controls that contain other elements and have a Content -property (for example, Button ). ContentPresenter is used inside control templates to display content.

What is ContentControl WPF?

Content Control is a base class that provides standardised functionality to WPF Controls. The Content Control class represents controls that can include a single item of content. This content is commonly plain text or a child control. Content Control is a subclass of the Control class in WPF.

What is DataTemplate WPF?

DataTemplate is about the presentation of data and is one of the many features provided by the WPF styling and templating model. For an introduction of the WPF styling and templating model, such as how to use a Style to set properties on controls, see the Styling and Templating topic.


1 Answers

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:

  • Template property type is ControlTemplate
  • ContentTemplate property type is DataTemplate and you can switch it in runtime using ContentControl.ContentTemplateSelector
like image 197
sll Avatar answered Sep 20 '22 07:09

sll