ContentPresenter.ContentSource vs Content

michael picture michael · Apr 8, 2011 · Viewed 8.3k times · Source

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?

Answer

H.B. picture H.B. · Apr 8, 2011

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.