Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I stumbled upon an article by Den Delimarsky on What is a ContentPresenter? which says:

In WPF there is an element called ContentPresenter, that is often used inside control templates, as well as inside the root application markup.

I don't understand the "inside the root application markup" part, because I thought that ContentPresenter can only used inside of a ControlTemplate.

The role of the ContentPresenter is quite clear when used in a ControlTemplate, but what are the valid reasons to use ContentPresenter outside of a template's markup?

share|improve this question
    
Hi Dreamer, this is on-topic here, but can you go a little more into what you're trying to do so people know how best to answer? –  user8 Oct 20 '11 at 19:33
    
I stumbled upon this article: dotnet.dzone.com/articles/what-contentpresenter which says "In WPF there is an element called ContentPresenter, that is often used inside control templates, as well as inside the root application markup. " and I didn't get that "inside the root application markup." part, because I though that ContentPresenter can only used inside of a ControlTemplate. Hence is my question. –  Dreamer Oct 25 '11 at 0:02
    
I added your explanation back into the question: thanks for clarifying. –  user8 Oct 25 '11 at 0:13
add comment

2 Answers

(This seems to be a quite outdated reply.)

There are valid uses of ContentPresenter everywhere. My primary use of it is to dispatch the view by the runtime type of the content.

Let me explain: imagine that you have a property of type Animal in your VM, so you'd like to display it somewhere in your UI. If you have a single UI for any kind of Animal, you can put it into a UserControl, and use a construct like

<AnimalControl DataContext="{Binding Animal}"/>

However imagine that you've got a bunch of different animals, say, Toads, Humans and Unicorns. So you'd like to differentiate between them (maybe you've developed several UserControls for them). You do it in the following way:

<ContentPresenter Content="{Binding Animal}"/>

In order for the correct view to be used, you need the following DataTemplates to be in scope:

<DataTemplate TargetType="{x:Type vm:Unicorn}">
    <UnicornControl/>
</DataTemplate>

etc. Usually people get such a functionality using TemplateSelector/ContentTemplateSelector, but this way is valid, too, and seems to be simpler (you can shuffle template scopes with custom styles, a la CSS).

share|improve this answer
add comment

There are no reasons. You should use ContentControl rather then ContentPresenter outside of a template.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.