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, ...).
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).
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With