Q:

Exception on adding ContentPresenter to a ContentTemplate

<ContentControl Content="Test">
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <Border>
                <ContentPresenter />
            </Border>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>

It throws stackoverflow exception. However if i use any other control else than ContentPresenter it works fine even ItemPresenter also works.I knows it doesnt make any sense to have ContentPresenter there but just for Knowledge want to know. Why it throws StackOverFlow exception and also Why does Intellisense shows it can be added(I mean it comes there in Intellisense that means syntatically its not wrong to have ContentPresenter there). Any help will be highly appericiated. Or is it any flaw in Wpf.

A:

The MSDN page for ContentPresenter says:

When a ContentPresenter object is in a ControlTemplate of a
  ContentControl, the Content, ContentTemplate, and
  ContentTemplateSelector properties get their values from the
  properties of the same names of the ContentControl. You can have the
  ContentPresenter property get the values of these properties from
  other properties of the templated parent by setting the ContentSource
  property or binding to them.

I imagine it gives a StackOverflow because it just keeps on trying to apply the template to the ContentPresenter in the ContentTemplate, and then on the next one inside it, and then the one inside it, etc.
You also can do things like the following, so this pattern probably isn't always wrong, even though IntelliSense probably doesn't do any checking for these sorts of scenarios.
<ContentControl Content="Test"> 
    <ContentControl.ContentTemplate> 
        <DataTemplate> 
            <Border> 
                <ContentPresenter ContentStringFormat="{}{0}" />
            </Border> 
        </DataTemplate> 
    </ContentControl.ContentTemplate> 
</ContentControl> 

