I'm not sure when I should use ContentPresenter
instead of ContentControl
(and vice-versa). At the moment, I'm using ContentControl
pretty much all the time in my DataTemplate
s. When would ContentPresenter
be a better choice? and why?
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).
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
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.
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.
ContentControl
is to make a WPF custom control (dont mix up with user control). So it is your top-level class. ContentPresenter
is just like any other "regular" control. You can use it in custom or user control or in a template or just in markup
Sometimes an example is easier than theoretical jargon. In an MS web site (Scroll to the bottom: http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v=vs.110).aspx), it uses a button as an example. A Button has a ContentControl, which allows you to place one control or a custom control that could be an Image, Text, CheckBox, StackPanel, Grid, whatever.
After the customization of Button, now on the Xaml, you can write
<my:Button>
<my:Button.Content>
<my:AnotherControl>
</my:Button.Content>
</my:Button>
In the above example code, the "my:Button.Content" is the ContentControl. The AnotherControl will be place to what you had specified where the ContentPresenter is.
Similarly, when compares TextBox and TextBlock, TextBox has a ContentPresenter for you to stuff stuff in it just like the above Button example whereas a TextBlock doesn't. A TextBlock only allows you to enter text.
Button
does not have a [ContentControl
](msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol(v=vs.110).aspx), it is a (inherits from) ContentControl
. The Button
has a ContentPresenter
. Note that you can do that with the standard Button
, no need to customize it. –
Chromosome ContentPresenter
, a ContentControl
could not be used just as well in the ControlTemplate
to display the content of the Button
. As such, it does not answer the question. –
Chromosome © 2022 - 2024 — McMap. All rights reserved.