WPF's presenter (ContentPresenter)

Source: Internet
Author: User

WPF's presenter (ContentPresenter)

2010-12-20 14:34 by Clingingboy, 10619 Reading, 3 Reviews, favorites, compilation

This is an article written 2 years ago.

Http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html

Let's look at MSDN's introduction to it first

Displays the content of a ContentControl

It seems that it is made for contentcontrol stature.

To understand this, first of all we have to understand the WPF content model, the above article has mentioned ContentControl inherited from control, more content properties, inherited from the ContentControl can be called the content model of the control.

It seems that the shadow of ContentPresenter is not visible here. Here are some examples

An example of a ContentPresenter

ContentPresenter can be output directly in XAML, and its visual tree contains a TextBlockthat does not find any style about ContentPresenter in the default WPF style definition. Illustrates that ContentPresenter is not a real code separation from the style, but instead provides a default template in the internal code that renders the TextBlock, if the template is created internally, It is common to create root elements using FrameworkElementFactory, but this approach is too complex for simple default actions, such as TextBlock inside ContentPresenter.

Then the problem comes out, why not directly with TextBlock, but also packaging a ContentPresenter?

ContentPresenter and TextBlock

To answer the above question, it is like to discuss the difference between the two, we look at the TextBlock

<Text= "Hello"/> 

TextBlock is a really text -themed element, and ContentPresenter's function is not just to render the text (only fill)

Just the default is to render the text, but the concept is completely different, the content property is an object type, not a string, you can redefine the template (ContentTemplate) with content as the data source, as shown in the following example

In this way, ContentPresenter will no longer be confined to the presentation of words.

Let's look at the relationship between ContentControl and ContentPresenter.

ContentControl and ContentPresenter

Look at a button style that has been redefined

As you can see, using ContentPresenter is very convenient, as long as you put ContentPresenter in the template, and do not need to do any additional binding (don't need to do it?). Just contentpresenter the internal help us do the default binding), but if you use TextBlock it is also necessary to do the binding

        <StyleTargetType= "button" > <setter property= "Template" > <setter.valuecontroltemplate targettype= " Button "> <textblock text = "{templatebinding content< Span style= "color:blue;" >} "/> </controltemplate> </setter.value> </setter > </style>   

As it seems, we might as well say that ContentControl is a special case of ContentPresenter, and Contentpresente

ContentPresenter is the foundation of ContentControl. in order to adapt Contentpresenter,contentcontrol to provide the relevant properties of the content model , essentially ContentPresenter is not just contentcontrol. ContentPresenter can bind the specified source property by specifying ContentSource

Remember that the content model is more than just rendering text, and it doesn't need to be contentpresenter if it's just to present the text.

relationship between parent and child elements (Itemspresenter)

Sometimes the control does not maintain its own logic, but relies on parent-child elements, such as the appeal of ContentPresenter, we also have a very common ListBox control , because inherit from ItemsControl, So there is a Itemspanel property as a collection element to host the container, but the collection control itself is not responsible for rendering the control, then this task is left to the child element Itemspresenter, in fact, it is very simple, as long as the Itemspresenter in an internal template, Itemspresenter detects if the parent element is a collection control and then adds Itemspanel to its internal visual tree

        <StyleX:Key="{X:TypeItemsControl}"TargetType="{X:TypeItemsControl} "> <SetterProperty= "Template" > <Setter.value> <ControlTemplateTargetType="{X:TypeItemsControl} "> <BorderBackground="{TemplateBindingBackground}"BorderBrush="{TemplateBindingBorderBrush}"BorderThickness="{TemplateBindingBorderThickness}"Padding="{templatebinding padding " Span style= "color:red;" >snapstodevicepixels= "true" > <itemspresenter Span style= "color:red;" >snapstodevicepixels= "{templatebinding snapstodevicepixels "/> </border> </controltemplate> </setter.value> </setter > </style>   

The following visual tree, StackPanel as the default container for ItemsControl

Let's get here first.

WPF's presenter (ContentPresenter)

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.