What's the difference between ContentControl and ContentPresenter?
Asked Answered
L

6

235

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 DataTemplates. When would ContentPresenter be a better choice? and why?

Laic answered 17/8, 2009 at 13:34 Comment(0)
G
196

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):

  1. Inside ControlTemplate use ContentPresenter
  2. Outside of ControlTemplate (including DataTemplate and outside templates) try not to use any of them, if you need to, you must prefer ContentPresenter
  3. 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).
Gudgeon answered 17/8, 2009 at 14:44 Comment(7)
Does that mean that, in general, I should probably use ContentPresenter inside my DataTemplates, because it's more light-weight (but functionally equivalent when used in a DataTemplate like this)? Then just use ContentControl as a base class if I'm writing a new control?Laic
I've edited the answer with more details when I would use ContentPresenter and when ContentControlGudgeon
Ok I got idea that ContentPresenter should be used in templates instead of ContentControl, but why?Quadruplex
@Quadruplex - ContentControl is the base class for every control that displays "content" (example: Label), ContentPresenter is the code used internally by ContentControl to display the content - so: 1. ContentPresenter is more lightweight, 2. ContentPresenter is designed to be used inside control templates and 3. ContnetPresenter is designed to be used as-is while ContentControl is designed to be extended (inherited from)Gudgeon
Pretty nice explanation you can find here: charly-studio.com/blog/contentcontrol-vs-contentpresenterMinaminabe
ContentPresenter behaves differently from ContentControl when it comes to having the Content property set. When you set ContentPresenter's Content property its DataContext changes to match the Content property, but ContentControl's DataContext remains unaffected. This matters if you have other properties on ContentPresenter set via binding, because once DataContext changes, all bindings use that as the source.Uncalledfor
I'm a little late to the party here. Great explanation @sll, Thanks!!!. I generally think of ContentPresenter like this: If the control using your template has a Content property (i.e. is a ContentControl), then the content you specify in your control will be presented at the location where ContentPresenter is located in your template.Sacci
P
26

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

Percuss answered 17/8, 2009 at 13:37 Comment(3)
Wouldn't a ContentPresenter also cause a DataTemplate to be applied to it's content? Isn't that one of its primary purposes?Gelya
mmm... yeah, probably. Anyway, Bea Stollnitz's explanation is much better than mine ;)Percuss
Your succinct answer seemed to sum it up quickly: I believe the whole design of the ContentPresenter is to simply "implement" the DataTemplate inflation --- it seems to have the sole job of just locating and inflating the template, setting the DataContext too; and trying then to just "disappear" as much as possible (THOUGH you STILL can bind within the inflated template to ambient properties like TextElement properties, coming then from the ContentPresenter). You don't need to worry about other things, and it just inflates the template in a relatively slim way. (I'm looking for the slimmest!)Crybaby
B
12

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.

Bodice answered 19/11, 2012 at 10:30 Comment(0)
U
2

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.

Unbuckle answered 25/4, 2014 at 21:29 Comment(0)
P
0

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

Piperidine answered 21/5, 2023 at 9:41 Comment(0)
T
-1

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.

Tensiometer answered 23/1, 2014 at 19:59 Comment(2)
A 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
But unrelated to that, this answer does not explain whether and why, instead of the 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.