0

I have a user control that should serve as container and it looks like following:

<ContentControl ... >
    <Grid>
        <Rectangle RadiusX="8" RadiusY="8">
            <Rectangle.Fill>
...
            </Rectangle.Fill>
        </Rectangle>

...

        <ContentPresenter />
    </Grid>
</ContentControl>

When I use it in a window, the contents of controls are replaced instead of put into ContentPresenter:

<XWpf2:MyContainer Margin="40">
        <ListView Margin="16" />
</XWpf2:MyContainer>

What is the right approach, what is wrong here?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Dusan
  • 5,000
  • 6
  • 41
  • 58

1 Answers1

2

You set the Content to a Grid, then you overwrite the Content with a ListView. If you want that structure to be preserved you need to assign it as the ContentControl.Template.

Of course the same thing can be said about the MyContainer if it is a UserControl, if you set the Content and then overwrite it with a ListView your whole ContentControl will be gone as well.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I get the contents of the user control when I put it inside ContentControl.Template but now I do not see the contents??? – Dusan May 06 '12 at 11:43
  • @Dusan: [This question](http://stackoverflow.com/questions/5758342/) might be helpful, you are probably still overwriting something somewhere. I can't divine your code... – H.B. May 06 '12 at 11:45
  • @HB - Solved, it seems that ContentPresenter must be bound when in template - Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}". Many Thanks! – Dusan May 06 '12 at 11:49
  • @Dusan: Did you perhaps not set the `ControlTemplate.TargetType`? For content controls that should hook up the `ContentPresenter` automatically. – H.B. May 06 '12 at 11:54
  • @HB - I did not set the TargetType initially. When I set it, it finally works as expected without binding on the ContentPresenter. Thanks again. – Dusan May 06 '12 at 11:58