Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentPresenter.ContentSource vs Content

Basically, I don't understand what the real difference here is:

The Microsoft code for TabItem uses:

<ContentPresenter ContentSource="Header" ... />

So, when would one use the Content property instead of (or in addition to) ContentSource?

like image 247
michael Avatar asked Apr 08 '11 20:04

michael


People also ask

What's the difference between ContentControl and ContentPresenter?

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 WPF ContentPresenter?

In WPF Content Presenter is a control that displays a single piece of content. In WPF Content Presenter is a control that displays a single piece of content. CONTENT PRESENTER: Content Presenter in WPF is used inside control templates, as well as inside the root application markup.


1 Answers

This property should only be used when the ContentPresenter is in a template. When a template contains a ContentPresenter with ContentSource set to "Abc", the Content, ContentTemplate, and ContentTemplateSelector properties of the ContentPresenter are automatically aliased to Abc, AbcTemplate, and AbcTemplateSelector, respectively. Beginning with the .NET Framework version 3.5 Service Pack 1, setting ContentSource to "Abc" also causes the ContentStringFormat property to be aliased to AbcStringFormat.

The two most useful values for this property are "Content" and "Header".

(MSDN)

ContentSource apparently sets more properties at once for convenience.


Practically, The declaration:

<ContentPresenter ContentSource="Header" />

Performs the following initialization.

<ContentPresenter Content="{TemplateBinding Header}"
                  ContentTemplate="{TemplateBinding HeaderTemplate}"
                  ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}"
                  ContentStringFormat="{TemplateBinding HeaderStringFormat}" />

It does this for each property separately only if the dependency property exists on the templated control.

like image 59
H.B. Avatar answered Sep 17 '22 01:09

H.B.