WPF: Why use contentpresenter. contentsource instead of the content attribute?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.