What's the difference between ContentControl and ContentPresenter?
Solution 1:
ContentControl
is a base class for controls that contain other elements and have a Content
-property (for example, Button
).
ContentPresenter
is used inside control templates to display content.
ContentControl
, when used directly (it's supposed to be used as a base class), has a control template that uses ContentPresenter to display it's content.
My rules of thumb (not applicable in every case, use your judgment):
- Inside
ControlTemplate
useContentPresenter
- Outside of
ControlTemplate
(includingDataTemplate
and outside templates) try not to use any of them, if you need to, you must preferContentPresenter
- Subclass
ContentControl
if you are creating a custom "lookless" control that host content and you can't get the same result by changing an existing control's template (that should be extremely rare).
Solution 2:
ContentPresenter is usually used in a ControlTemplate, as a placeholder to say "put the actual content here".
A ContentControl can be used anywhere, not necessarily in a template. It will pick up any DataTemplate defined for the type of content assigned to it
Solution 3:
I recently wrote a post on my blog regarding these two controls:
ContentPresenter vs ContentControl
The ContentPresenter.ContentSource is what actually makes the biggest difference between the two classes.
ContentSource property makes sense only within a ControlTemplate; it determines which TemplatedParent property the content should be mapped with.
For example, if a control contains a dependency property MyProperty1
, then we might find the following within its ControlTemplate
:
<ControlTemplate TargetType="MyControl" >
[...]
<ContentPresenter ContentSource="MyProperty1" />
[...]
</ControlTemplate>
The content of the ContentPresenter will receive the value of MyProperty1
.
Please note that if the name of the property is Content
, there is no need to specify ContentSource
as it is the default value.
For those who know angularJs: this is similar to transclude mecanism.
Solution 4:
Its an old question but I was just finishing developing an animated Tile Control, template based for a universal app, look at this code from the old Phone WP7/8 SDK:
<ContentControl x:Name="contentControl" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch">
<ContentPresenter x:Name="contentPresenter" CacheMode="BitmapCache"/>
</ContentControl>
Here you can see the ContentControl is the Container and the Presenter for displaying content. In most cases the ControlTemplate will be the Container but if you want in your ControlTemplate
another container you can put an extra Container: ContentControl
in it and for presenting the content a separate ContentPresenter
. If you dont need a separate container then just use ControlTemplate
and ControlPresenters
for displaying content blocks at least thats what the guys at Microsoft did when they developed the WP7/8 SDK. The ContentControl can also be used for displaying content but then it serves both as container and presenter. So in the sample code above its purpose is splitted in Container and Presenter. In dynamic samples you could display the container (it can have an empty background or something thats not there yet) and then dynamically fill it with the presenter content. A container has dimensions (width,height etc.), you put those properties on the container control and present content on it. In the sample the ContentControl determines what has to be done with the presenter content.