Because contentpresenter. contentsource is more convenient than adding a templatebinding to the content attribute? More than that. In fact, if contentsource is used, all content-related attributes such as contentstringformat, contenttemplate, and contenttemplateselector will be set automatically, you must manually bind the property to contentpresenter.
For example, use contentpresenter's contentsource and set contentstringformat in contentcontrol:
<Window. Resources>
<Style targettype = "contentcontrol">
<Setter property = "template">
<Setter. value>
<Controltemplate targettype = "contentcontrol">
<! -- This is equivalent to <contentpresenter/> -->
<! -- Emphasize that the contentpresenter's contentsource attribute is content. -->
<Contentpresenter contentsource = "content"/>
</Controltemplate>
</Setter. value>
</Setter>
</Style>
</Window. Resources>
<Contentcontrol contentstringformat = "Hello: {0}"> mgen </contentcontrol>
The result will be output: Hello, mgen.
If you change contentpresenter to templatebinding, bind the content attribute of contentcontrol:
<Contentpresenter content = "{templatebinding content}"/>
The result will only be output: mgen.
In this case, contentstringformat, contenttemplate, and contenttemplateselector do not work, so you can only use templatebinding to bind them to contentpresenter:
<Contentpresenter content = "{templatebinding content }"
Contentstringformat = "{templatebinding contentstringformat }"
Contenttemplate = "{templatebinding contenttemplate }"
Contenttemplateselector = "{templatebinding contenttemplateselector}"/>
Of course, contentpresenter is not limited to contentcontrol, but can be used in any control attributes such as contentcontrol. content, such as headeredcontentcontrol. header.
In this way, the control template of headeredcontentcontrol is defined:
<Style targettype = "headeredcontentcontrol">
<Setter property = "template">
<Setter. value>
<Controltemplate targettype = "headeredcontentcontrol">
<Dockpanel>
<Border dockpanel. Dock = "TOP">
<Contentpresenter contentsource = "Header"/>
</Border>
<! -- Equal to: <contentpresenter contentsource = "content"/> -->
<Contentpresenter/>
</Dockpanel>
</Controltemplate>
</Setter. value>
</Setter>
</Style>
Example:
<Headeredcontentcontrol header = "Header"
Content = "content"
Headerstringformat = "Up: {0 }"
Contentstringformat = "lower: {0}"/>
Result:
If you use content to bind the header property:
<Contentpresenter content = "{templatebinding header}"/>
You have to bind the contentstringformat, contenttemplate, and contenttemplateselector attributes again, so remember to always use the contentpresenter. contentsource attribute.