2012-05-06 24 views
0

我有应作为容器的用户控件,它看起来像如下:WPF容器控件

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

... 

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

当我在一个窗口中使用它,控件的内容被替换,而不是投入ContentPresenter:

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

什么是正确的方法,这里有什么问题?

+0

相似的问题:http://stackoverflow.com/q/10269284 –

回答

2

您将Content设置为Grid,然后用ListView覆盖Content。如果您希望保留该结构,则需要将其指定为ContentControl.Template

当然,同样的事情可以说对MyContainer如果是UserControl,如果设置Content,然后用ListView将其覆盖你的整个ContentControl将离开为好。

+0

当我把它放在ContentControl.Template中时,我得到了用户控件的内容,但现在我看不到内容了? – Dusan

+0

@Dusan:[这个问题](http://stackoverflow.com/questions/5758342/)可能会有所帮助,你可能仍然在某处覆盖某些东西。我无法推测你的代码... –

+0

@HB - 解决了,似乎ContentPresenter必须在模板中绑定 - Content =“{Binding RelativeSource = {RelativeSource TemplatedParent},Path = Content}”。非常感谢! – Dusan