Because Contentpresenter.contentsource is more convenient than adding a templatebinding to the content property? Not only that, actually, if you use ContentSource, Content-related properties such as Contentstringformat,contenttemplate and Contenttemplateselector are automatically set, and if you use content plus templatebinding, You must manually bind the above attributes to the ContentPresenter.
For example, use ContentPresenter's ContentSource, and then set the Contentstringformat in ContentControl:
<Window.Resources>
<style targettype= "ContentControl" >
<setter property= "Template" >
<Setter.Value>
<controltemplate targettype= "ContentControl" >
<!--here is equivalent to direct <contentpresenter/>--
<!--emphasize the direct use of ContentPresenter its ContentSource attribute is content--
<contentpresenter contentsource= "Content"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<contentcontrol contentstringformat= "Hello: {0}" >Mgen</ContentControl>
The result is output: Hello: MGen.
If you switch the above ContentPresenter to the Content property of TemplateBinding bound ContentControl: <contentpresenter content= "{TemplateBinding Content} "/>
The result will only output: MGen.
In fact, contentstringformat,contenttemplate and Contenttemplateselector are not going to work, Then we can only use TemplateBinding to bind them in the ContentPresenter.
<contentpresenter content= "{TemplateBinding Content}"
contentstringformat= "{TemplateBinding Contentstringformat}"
Contenttemplate= "{TemplateBinding ContentTemplate}"
Contenttemplateselector= "{TemplateBinding contenttemplateselector}"/>
Of course ContentPresenter is not limited to ContentControl, and can be used in any control property like Contentcontrol.content, such as the Headeredcontentcontrol.header attribute.
This defines the HeaderedContentControl control template:
<style targettype= "HeaderedContentControl" >
<setter property= "Template" >
<Setter.Value>
<controltemplate targettype= "HeaderedContentControl" >
<DockPanel>
<border dockpanel.dock= "Top" >
<contentpresenter contentsource= "Header"/>
</Border>
<!--equals: <contentpresenter contentsource= "Content"/>--
<ContentPresenter/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Example:
Content= "Content"
Headerstringformat= "on: {0}"
Contentstringformat= "under: {0}"/>
Results:
If you use Content to bind the Header property: <contentpresenter content= "{TemplateBinding header}"/>
Then you have to bind the Contentstringformat,contenttemplate and Contenttemplateselector properties again, So remember to always use the Contentpresenter.contentsource property.