WPF: Why use Contentpresenter.contentsource instead of the content property?

Source: Internet
Author: User
Tags bind
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.

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.