tags:

views:

2403

answers:

2

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?

I've read http://www.beacosta.com/blog/?m=200611 but I still don't get when/why I would choose one over the other.

+1  A: 

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

Thomas Levesque
Wouldn't a ContentPresenter also cause a DataTemplate to be applied to it's content? Isn't that one of its primary purposes?
Drew Noakes
mmm... yeah, probably. Anyway, Bea Stollnitz's explanation is much better than mine ;)
Thomas Levesque
+12  A: 

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.

EDIT: 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 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).
Nir
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?
Wilka
I've edited the answer with more details when I would use ContentPresenter and when ContentControl
Nir